Java escape wildcards regular the expression escape character

Source: Internet
Author: User

For example, the standard syntax for a regular expression with the same 3-bit number is:

([\d]) \1{2}

However, if you write this in Java code, you will see a syntax error, as follows:

String regEx = "([\d]) \1{2}";

Consider that the \ symbol is an escape character in the syntax of the Java string, so the Java syntax that is required is:

String regEx = "([\\d]) \\1{2}";

To deepen your impressions, take a look at the following example: the standard syntax for regular expressions with successive identical three-bit characters, preceded by the same 3-digit number, is:

(\d) \1{2} (.) \2{2}

And in Java, it's written like this:

String regEx = "(\\d) \\1{2} (.) \\2{2} ";

Note that \1 and \2 refer to the location of the Patten.

Finally, the whole test code is given:

@Test
public void test02 () {
The string to validate
String str = "111";
Regular expression rules
String regEx = "([\\d]) \\1{2}";
String regEx = "([\d]) \1{2}";
Compiling regular expressions
Pattern pattern = pattern.compile (regEx);
Ignore case-written notation
Pattern Pat = Pattern.compile (regEx, pattern.case_insensitive);
Matcher Matcher = Pattern.matcher (str);
Find the character/string in a string that matches a regular expression
Boolean rs = Matcher.find ();
System.out.println (RS);
}

Java escape wildcards regular the expression escape character

Related Article

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.