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