Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic knowledge of Java regular expression

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the basic knowledge of Java regular expression", which is easy to understand and well organized. I hope it can help you solve your doubts. Let me lead you to study and learn "what is the basic knowledge of Java regular expression".

As we all know, in program development, it is inevitable to encounter situations that need to match, find, replace and judge strings, and these situations are sometimes more complex, if solved by pure coding, it is often a waste of programmers' time and energy. Therefore, learning and using regular expressions has become the main means to solve this contradiction.

As we all know, a regular expression is a specification that can be used for pattern matching and substitution. a regular expression is a text pattern consisting of ordinary characters (such as characters a to z) and special characters (metacharacters). It is used to describe one or more strings to match when looking for the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.

One: what is a regular expression

1. Definition: a regular expression is a specification that can be used for pattern matching and substitution. a regular expression is a text pattern consisting of ordinary characters (such as characters a to z) and special characters (metacharacters). It is used to describe one or more strings to match when looking for the body of the text. The regular expression acts as a template that matches a character pattern with the searched string.

two。 Purpose:

String matching (character matching)

String lookup

String substitution

String segmentation

For example:

Extract the email address from the web page

Is the IP address correct?

Pull a link from a web page

Classes in 3.java that handle regular expressions:

Java.lang.String

Java.util.regex.Pattern: pattern class: a pattern where strings are matched. The pattern itself has been compiled and is much more efficient to use.

Java.util.regex.Matcher: matching class: this pattern matches the result of a string, which may have many results.

4: here's a brief introduction to regular expressions through a Mini Program.

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main (String [] args) {/ / matches () determines whether the string matches an expression, "." Represents any character p ("abc" .characters ("...")); / / replaces the number in the string "a2389a" with *, and\ d represents the "0muri 9" number p ("a2389a" .replaceAll ("\\ d", "*")) / / compile any string of length 3 that is a murz, which can speed up the matching speed Pattern p = Pattern.compile ("[Amurz] {3}"); / / match and put the match result in the Matcher object Matcher m = p.matcher ("abc"); p (m.matches ()) / / the above three lines of code can replace p ("abc" .codes ("[a murz] {3}"));} public static void p (Object o) {System.out.println (o);}}

Here are the printed results

True

A few years ago.

True

True

Now we use some experiments to illustrate the matching rules of regular expressions, here is the Greedy way

. Any character

A? An once or not once

A * a zero or more times

A + an one or more times

A {n}? An exactly n times

A {n,}? An at least n times

A {n,m}? An at least n times, but not more than m times

/ / preliminary understanding. * +?

P ("a" .cake (".")); / / true

P ("aa" .vendor ("aa")); / / true

P ("aaaa" .vendor ("a *")); / / true

P ("aaaa" .vendor ("a +")); / / true

P ("" .vendor ("a*")); / / true

P ("aaaa" .vendor ("a?")); / / false

P ("" .buyer ("a?")); / / true

P ("a" .salary ("a?")); / / true

P ("1232435463685899" .notify ("\ d {3100}")); / / true

P ("192.168.0.aaa" .minutes ("\\ d {1pr 3}\\.\ d {1J 3}\.\ d {1J 3}\ d {1J 3}")); / / false

P ("192" .minutes ("[0-2] [0-9] [0-9]")); / / true

[abc] a, b or c (simple class)

[^ abc] any character except a, b, or c (negative)

[a-zA-Z] A to z or A to Z, with letters at both ends included (range)

[a Muir d [m Mui p]] a to d or m to p: [a-dm-p] (union)

[a Mustang & [def]] d, e or f (intersection)

[a Mustang & [^ BC]] a to z, except b and c: [ad-z] (minus)

[a Mustang & [^ mmurp]] a to z instead of m to p: [a-lq-z] (minus)

/ / range

P ("a" .vendor ("[abc]")); / / true

P ("a" .vendor ("[^ abc]")); / / false

P ("A" .vendor ("[a-zA-Z]")); / / true

P ("A" .buyer ("[Amurz] | [Amurz]")); / / true

P ("A" .hammer ("[Amurz [Amurz]]")); / / true

P ("R" .hammer ("[Amurzilla & [RFG]]")); / / true

\ d number: [0-9]

\ d non-numeric: [^ 0-9]

\ s white space character: [\ t\ n\ x0B\ f\ r]

\ s non-white space character: [^\ s]

\ W word character: [a-zA-Z_0-9]

\ W non-word characters: [^\ w]

/ / know\ s\ w\ d\

P ("\ n\ r\ t" .vendor ("\\ s (4)")); / / false

P ("" .vendor ("\\ S")); / / false

P ("axi8" .cake ("\\ w (3)"); / / false

P ("abc888& ^%" .colors ("[amurz] {1Magne3}\\ d + [& ^ #%] +"); / / true

P ("\\" .resume ("\")); / / true

Boundary matcher

The beginning of the ^ line

The end of the $line

\ b word boundary

\ B non-word boundary

\ a the beginning of the input

A matching ending on\ G

The end of\ Z input, only for the last Terminator (if any)

\ z the end of the input

/ / Boundary matching

P ("hello sir" .minutes ("^ h.*")); / / true

P ("hello sir" .minutes (". * ir$")); / / true

P ("hello sir" .colors ("^ h [a murz] {1pr 3} o\\ b.*")); / / true

P ("hellosir" .colors ("^ h [a murz] {1pr 3} o\\ b.*")); / / false

/ / Blank lines: one or more (blank and non-newline characters) begin and end with newline characters

P ("\ n" .resume ("^ [\\ sstories & [^\\ n]] *\\ n$")); / / true

Method analysis

Matches (): matches the entire string

Find (): match substring

LookingAt (): always match from the beginning of the entire string

/ / email

P ("asdsfdfagf@adsdsfd.com" .conversation ("[\ w [. -]] + @ [\\ w [. -]] +\ .[\ w] +")); / / true

/ / matches () find () lookingAt ()

Pattern p = Pattern.compile ("\\ d {3jin5}")

Matcher m = p.matcher ("123-34345-234-00")

/ / use the regular expression engine to find a match for the whole "34345-234-00". When the first "-" does not match, stop.

/ / but will not spit out the mismatched "-".

P (m.matches ())

/ / spit out the mismatched "-"

M.reset ()

/ / 1: when preceded by p (m.matches ()); look for substrings starting with ". 34345-234-00"

/ / it will be the first and second two to find "34345" and "234" and the last two will not be found as false

/ / 2: when preceded by p (m.matches ()); and m.reset (); look for substrings starting with "123-34345-234-00"

/ / will be true,true,true,false

P (m.find ())

P (m.start () + "- -" + m.end ())

P (m.find ())

P (m.start () + "- -" + m.end ())

P (m.find ())

P (m.start () + "- -" + m.end ())

P (m.find ())

/ / if it is not found, an exception java.lang.IllegalStateException will be reported.

/ / p (m.start () + "- -" + m.end ())

P (m.lookingAt ())

P (m.lookingAt ())

P (m.lookingAt ())

P (m.lookingAt ())

String substitution: the following method is very flexible for string substitution

/ / string substitution

/ / Pattern.CASE_INSENSITIVE is case insensitive

Pattern p = Pattern.compile ("java", Pattern.CASE_INSENSITIVE)

Matcher m = p.matcher ("java Java jAva ILoveJavA youHateJAVA adsdsfd")

/ / Storage string

StringBuffer buf = new StringBuffer ()

/ / count odd and even numbers

Int I = 0

While (m.find ()) {

ITunes +

If (I% 2 = = 0) {

M.appendReplacement (buf, "java")

} else {

M.appendReplacement (buf, "JAVA")

}

}

/ / without this sentence, the string adsdsfd will be discarded

M.appendTail (buf)

P (buf)

Result print:

JAVA java JAVA ILovejava youHateJAVA adsdsfd

Grouping

/ / group grouping, grouping with ()

Pattern p = Pattern.compile ("(\ d {3jin5}) ([amurz] {2})")

String s = "123aa-34345bb-234cc-00"

Matcher m = p.matcher (s)

P (m.groupCount ()); / / 2 groups

While (m.find ()) {

P (m.group ()); / / both have numeric letters.

/ / p (m.group (1)); / / numbers only

/ / p (m.group (2)); / / only letters

}

These are all the contents of the article "what are the basics of Java regular expressions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report