INTRODUCTION TO REGEX EXPRESSIONS FOR JAVA DEVELOPERS

QUANTIFIERS ( * + ? {n} . )

The quantifiers in Java ( and in most programming languages) consists of *, +, ?, {}.

OPERATORS(| [] ^ )

Pipe (|) is used as ‘OR’. So it matches the expression on the left or the expression on the right. e.g abc|abd means the text should be either abc or abd. TRY IT.

GROUPING AND BACK REFERENCES

Another interesting thing about regex is that you can group your expressions and probably reuse them when necessary. To group elements in expressions, use round brackets e.g ().

private static String backReference(String text) {
String pattern = "(\\w+)([\\s])";
return text.replaceAll(pattern, "$1");
}

LOOK AHEAD/LOOK BEHIND

This is a way to exclude a pattern. So you can say a string should be valid only if it doesn’t come before a particular string and vice versa. e.g. abc(?=d) will match ‘abc’ as long as it follows a ‘d’. but note that ‘d’ will be excluded. TRY IT.

GREEDY AND LAZY

The quantifiers above are said to be greedy in the sense that they’ll match as many occurrences as long as the match is still successful. e.g for the regex a.+c, you would expect it to mean the text should be ‘a’ followed by one or more non-white space character. So you would expect a possible match to be ‘abcccd’ or ‘abcc’ or ‘abbc’ as individual results. This is meant to be true but because of the greedy attitude, it will grab all the texts (abcccd abcc abbc) as one and return ‘abcccd abcc abbc’ as one result because if you notice, the first character is ‘a’ followed by one or more of any other character and it now ends with c which matches exactly a.+c.

CHARACTER CLASSES

A character class is an escape sequence that represents a group of characters. Some predefined characters in java are listed below:

NOW OVER TO THE WORLD OF JAVA

image from Google
public static void main(String[] args) {
String value = "12345";
System.out.println("The Result is: "+value.matches("\\d{5}"));
}
public static void main(String[] args) {
String value = "12345";
String regex = "\\d{5}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
System.out.println("The Result is: "+matcher.matches());

}
public class PatternMatcher {
public static void main(String[] args) {
String [] texts = {"nozzle","punjabi","waterlogged","imprison","crux","numismatlogists","sultans","rambles","deprecating","aware","outfield","marlborough","guardrooms","roast","wattage","shortcuts","confidential","reprint","foxtrot","disposseslogsion","floodgate","unfriendliest","semimonthlies","dwellers","walkways","wastrels","dippers","engrlogossing","undertakings"}; List<String> resultStringMatches = matchUsingStringMatches(texts);
List<String> resultsPatternMatcher=matchUsingPatternMatcher(texts);
System.out.println("List: "+resultStringMatches.toString());
System.out.println("List Pattern/Matcher: "+resultStringMatches.toString());
}

private static List<String> matchUsingPatternMatcher(String[] texts) {
List<String> matchedList = new ArrayList<>();
String pattern = "[a-zA-Z]*log[a-zA-Z]*";
Pattern regexPattern = Pattern.compile(pattern);
Matcher matcher;
for (int i = 0; i < texts.length; i++) {
matcher = regexPattern.matcher(texts[i]);
if (matcher.matches()) {
matchedList.add(texts[i]);
}
}
return matchedList;
}
private static List<String> matchUsingStringMatches(String[] texts) {
String pattern = "[a-zA-Z]*log[a-zA-Z]*";
List<String> matchedList = new ArrayList<>();

for (int i = 0; i < texts.length; i++) {
//each time it calls matches(pattern),
//it creates a pattern object
if (texts[i].matches(pattern)) {
matchedList.add(texts[i]);
}
}
return matchedList;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store