Java Web regular expression,

Source: Internet
Author: User

Java Web regular expression,

For the web, string processing is particularly important, while regular expressions are a powerful tool for string processing. They can be seen in character filtering and verification.

Today, we need to process a json String. When we use String. replaceAll, we encounter an embarrassing situation where regular expressions are not written. So let's simply add the knowledge of regular expressions.

Start with the use of a regular expression.

Import java. util. regex. matcher; import java. util. regex. pattern; public class Test {public static void main (String [] args) {// This regular expression indicates matching String regex = "\ d {3, 4}-\ d {7}" such as 1254-8888888,125-6966356 }"; // initial String str = "agdf/1254-8888888sssdf125-6966356"; String aft = str. replaceAll (regex, "replace"); System. out. println ("repalceAll after =" + aft); Pattern p = Pattern. compile (regex); Matcher m = p. matcher (str); // m. if find () is true, it always matches. find () {// because the regular expression does not contain () parentheses, the number of capture groups is 0 System. out. println ("Number of capturing groups, m. groupCount = "+ m. groupCount (); // m. group () is equivalent to m. group (0), corresponding to groupCount, that is, direct matching, not grouping System. out. println ("m. group = "+ m. group (0 ));}}}

Running result

After repalceAll ===agdf/replace sssdf replace m. group === 1254-8888888 Number of capturing groups, m. groupCount = 0 m. group === 125-6966356 Number of capturing groups, m. groupCount = 0

Starting from the regular expression, String regex = "\ d {3, 4}-\ d {7 }";

"\ D" is a java escape. The original regular expression should be "\ d", indicating matching numbers from 0 to 9, it can also be written as [0-9]. I have never tried it.

"\ D {3, 4}" indicates that there are 3 to 4 numbers of \ d, that is, matching 123,3212, 000, followed by "\ d {7, matches 8888888 characters.

For grouping, see the demo below.

Import java. util. regex. matcher; import java. util. regex. pattern; public class Test {public static void main (String [] args) {// This regular expression indicates matching String regex = "([a-zA-Z] +) (\ d {7})" such as 1254-8888888,125-6966356 })"; // initial String str = "agdf12548888888sssdf1231666356"; Pattern p = Pattern. compile (regex); Matcher m = p. matcher (str); // m. if find () is true, it always matches. find () {// because the regular expression does not contain () parentheses, the number of capture groups is 0 System. out. println ("Number of capturing groups, m. groupCount = "+ m. groupCount (); // m. group () is equivalent to m. group (0), corresponding to groupCount, that is, direct matching, not grouping System. out. println ("m. group (0) = "+ m. group (0); // corresponds to the first bracket ([a-zA-Z] +) System. out. println ("m. group (1) = "+ m. group (1); // corresponds to the second bracket (\ d {7}) System. out. println ("m. group (2) = "+ m. group (2); System. out. println ("================= I am a line break ================ ");}}}

Running result

Number of capturing groups, m. groupCount = 2 m. group (0) === AGdf1254888m. group (1) = AGdfm. group (2) = 1254888 =, m. groupCount = 2 m. group (0) === sssdf121_66m. group (1) === sssdfm. group (2) = 1256966 =

At last, it comes with the common Regular Expression characters.

Character value range
1. [abc]: It may be a, B, or c.
2. [^ abc]: indicates that it is not any one of a, B, and c.
3. [a-zA-Z]: an English letter
4. [0-9]: indicates a number.

Concise character representation
.: Match any character
\ D: Number
\ D: Non-numeric
\ S: It is composed of null characters, [\ t \ n \ r \ x \ f]
\ S: It is composed of non-null characters. [^ \ s]
\ W: letters, numbers, underscores, [a-zA-Z0-9 _]
\ W: not composed of letters, numbers, and underscores

Quantity expression
1 .? : 0 or 1 occurrence
2. +: indicates one or more occurrences.
3. *: Indicates 0, 1, or multiple occurrences.
4. {n}: n times
5. {n, m}: n ~ MB
6. {n ,}: n or more times

Logical expression
1. XY: indicates that X is followed by Y. Here, X and Y are part of the regular expression respectively.
2. X | Y: Indicates X or Y. For example, "food | f" matches foo (d or f), while "(food) | f" matches food or f.
3. (X): a subexpression that treats X as a whole.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.