Pattern matcher
/**
* The Pattern matches() returns true only when preciseley matches the pattern.
* To check only parts of the string find() is used.
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pattern_matcher {
public static void main(String[] args) {
Pattern p = Pattern.compile("Version"); // regex pattern
Matcher m = p.matcher("Version 1.0"); // content
Boolean match_exactly = m.matches();
Boolean found_parts = m.find();
System.out.println(match_exactly); // false
System.out.println(found_parts); // true
}
}
Greedy
/**
* Greedy behavior match the longest sequence.
* Use "?" modifier for ungreedy behavior.
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Greedy {
public static void main(String[] args) {
Pattern p;
Matcher m;
String txt = "extend cup end table";
// Greedy
p = Pattern.compile("e.+d");
m = p.matcher(txt);
while(m.find()) {
System.out.println(m.group()); // extend cup end
}
// Ungreedy
p = Pattern.compile("e.+?d");
m = p.matcher(txt);
while(m.find()) {
System.out.println(m.group()); // extend
// end
}
// Group example
p = Pattern.compile("Java ?(8|SE)");
m = p.matcher("Java 8 Java SE");
while(m.find()) {
System.out.println(m.group()); // Java 8
// Java SE
}
}
}
Replace
Replace every sequence that matches the pattern with the replacement string.
/**
* Matcher replaceAll()
*
* Replace the matches from pattern with the provided string
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Replace {
public static void main(String[] args) {
Pattern p = Pattern.compile("abc.*? ");
Matcher m = p.matcher("abc abcd bcd cef");
// abc, char (zero or more), ungreedy, space
while(m.find()) {
String r = m.replaceAll("AAA "); // Look Here
System.out.println(r); // AAA AAA bcd cef
}
}
}
Split
Get the text found on either side of the pattern.
/**
* Pattern, String split()
* Break the string in parts using regex pattern
* Split by char
* Split by camer case
*/
package com.minte9.basics.regexp;
import java.util.regex.Pattern;
public class Split {
public static void main(String[] args) {
String[] words;
Pattern p = Pattern.compile("[ ,.!]");
words = p.split("one two,alpha9 12!done.");
for (String w:words) {
System.out.println(w); // one two alpha9 12 done
}
words = "AbCdEf".split("(?=[A-Z])"); // Look Ahead
for(String w:words) {
System.out.println(w); // Ab Cd Ef
}
}
}
Lookaround
Lookarounds are not included in the match.T
package com.minte9.basics.regexp;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class LookaroundTest {
public static boolean find(String regex, String str) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
@Test public void lookBehind() {
assertTrue(find("(?<=a)x", "axyz")); // Look behind for a (true)
assertFalse(find("(?<=b)x", "axyz")); // Look befind for a (false)
}
@Test public void lookAhead() {
assertTrue(find("(?=x)xyz", "axyz")); // Look ahead for x (true)
assertFalse(find("(?=x)ax", "axyz")); // Look ahead for x (false)
}
@Test public void lookBehindNegative() {
assertFalse(find("(!?<=a)x", "ax")); // Look behind for !a (false)
}
@Test public void lookAheadNegative() {
assertFalse(find("(?!x)x", "ax")); // Look ahead for !x (false)
}
}
Questions and answers:
Clink on Option to Answer
1. matches() return true
- a) when pattern match
- b) when pattern is matched precisely
2. The regex patterns are greedy
- a) by default
- b) false
3. Lookaheads are not included in the match
- a) false
- b) true