# Basic Regular Expression in Java

## A - Regular Expression Overview

Regular expression (Regex) is a sequence of characters that define a search pattern for strings. Regular expression can be used to search, edit and manipulate text and data.
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.

## B - Regular Expression in Java

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.

## C - Regular Expression syntax

 Regular Expression Descriptions . Matches any character ^kai Matches any text that start with kai kai$Matches any text that end with kai [xyz] Matches the letter x or y or z [abc][xyz] Matches a or b or c followed by x or y or z [^xyz] Matches any text except x or y or z. The ^ in [] negates the regex. [a-z0-9] Matches letter from a to z or digit from 0 to 9 x|y Matches letter x or y xy Matches letter x followed by y ### 2 - Metacharacters  Regular Expression Descriptions \d Matches any digit, short for [0-9] \D Matches any non-digit \s Matches any whitespace character, short for [ \t\n\x0b\r\f] \S Matches any non-whitespace character \s+ Matches several white space characters \w Matches any word character, short for[a-zA-Z0-9] \W Matches any non-word character ### 3 - Quantifier  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 ### 4 - Grouping in Regular Expression 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]

## C - Some examples about Regular Expression in 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;

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;

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 :)