INTRODUCTION TO REGEX EXPRESSIONS FOR JAVA DEVELOPERS

QUANTIFIERS ( * + ? {n} . )

OPERATORS(| [] ^ )

GROUPING AND BACK REFERENCES

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

LOOK AHEAD/LOOK BEHIND

GREEDY AND LAZY

CHARACTER CLASSES

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;
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create and publish your package into PyPI

Debugging with Science!

Updated Website!

Read Database tables into Pandas DataFrame

5 Best Practices for Successful Software Documentation | miraminds

My journey as a non-circuital student to becoming a software engineer

Verification and Validation: What’s The Difference? #QuickRead

What does 101 means in a course title?

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
Chunks

Chunks

More from Medium

Clean Code book — Part 1

Predefined Functional Interfaces

Java — Environment Setup

What is java?