The pattern defined by regular expression can match zero, one or several times in a string. The regular expression pattern will be applied to the string from left to right. Any character that has been used in a match cannot be reused.
For example: regular expression 121 will match string 12121212 only twice.
In Java, the java.util.regex package consists of 3 classes to work with regular expression:
- Â Pattern: represents a regular expression. To create a Pattern object, you have to invoke a static method that accept regular expression string as an argument .
[java]Pattern mPattern = Pattern.compile(pattern);[/java]
- Â Matcher:Â interprets the pattern and performs match operations against an input string. Matcher object can be obtained by invoking Matcher method on Pattern object ().
[java]Matcher mMatcher = mPattern.matcher(EXAMPLE_TEST);[/java]
- Â Â PatternSyntaxException:Â unchecked exception that indicates a syntax error in a regular expression pattern.
 Regular Expression |  Descriptions |
 * |  Repeats 0 or many times |
 + | Repeats 1 or many times |
 ? | Repeats 0 or 1 time |
 {x} | Repeats x times |
 {x,y} | Repeats from x to y times |
You can group a part of regular expression and refer to it later using $ character.
For example: Regular expression to remove all odd white spaces between 2 words:
[java] String pattern = “(\S)(\s+)(\S)”; System.out.println(myString.replaceAll(pattern, “$1 $3”)); [/java]
Negative lookahead provides the possibility to exclude a pattern. With this you can say that a string should not be followed by another string.
Negative Lookaheads are defined via (?!pattern)
.
For example, the following will match “kai” if “kai” is not followed by “other”.
[java]kai(?!other)[/java]
Check if the input text is 24 or 32 bit hex color, with an optional leading # or ox:
[java] public void checkValid24or32bitColorFormat(){ Pattern pattern; Matcher matcher; String TEXT;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pattern = Pattern.compile("(?:#|0x)?(?:[0-9A-F]{2}){3,4}");
TEXT = "0xF0F73611";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches()));
TEXT = "#FF006C";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches()));
TEXT = "99AAB7FF";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches()));
TEXT = "FFZZ08";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches())); } [/java]
Check if the input text is a “slug” text or not:
[java] public void checkSlugText(){ Pattern pattern; Matcher matcher; String TEXT;
1
2
3
4
5
6
7
8
9
pattern = Pattern.compile("^[a-z0-9-]+$");
TEXT = "a_b_123";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches()));
TEXT = "a-b-123";
matcher = pattern.matcher(TEXT);
System.out.println(TEXT + " : " + String.valueOf(matcher.matches())); } [/java]
So, that’s all for the basic regular expression in Java. I’m a newbie to regular expression so please don’t mind telling me if I have something wrong in this post!
Thanks :)
Â
Comments powered by Disqus.