String regex = "(?=[a-z])";
String input = "123a567b9";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
a(?=b) -- positive lookahead: Is there any item matches 'a' which is followed by 'b'?
a(?!b) -- negative lookahead: Is there any item matches 'a' which is NOT followed by 'b'?
(?<=a)b -- positive lookbehind: Is there any item matches 'b' which is led by 'a'?
(?<!a)b -- negative lookbehind: Is there any item matches 'b' which is NOT led by 'a'?
The simpler forms:
(?=x) -- positive lookahead: Is there any item matches '' which is followed by 'x'?
(?!x) -- negative lookahead: Is there any item matches '' which is NOT followed by 'x'?
(?<=x) -- positive lookbehind: Is there any item matches '' which is led by 'x'?
(?<!x) -- negative lookbehind: Is there any item matches '' which is NOT led by 'x'?
Example: Lookahead assertion -- Find the 'black' color used for 'cat'.
String regex = "black(?= cat)";
String input = "The black cat wears a black hat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
The ouput:
Regex: black(?= cat)
Input: The black cat wears a black hat.
Found 'black' from 4 to 9
Example: Lookahead assertion -- Find the 'black' color used NOT for 'cat'.
String regex = "black(?! cat)";
String input = "The black cat wears a black hat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
The ouput:
Regex: black(?! cat)
Input: The black cat wears a black hat.
Found 'black' from 22 to 27
Example: Lookbehind assertion -- Find the 'cat' with 'black' color.
String regex = "(?<=black )cat";
String input = "The black cat and the white cat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
The ouput:
Regex: (?<=black )cat
Input: The black cat and the white cat.
Found 'cat' from 10 to 13
Example: Lookbehind assertion -- Find the 'cat' with NOT 'black' color.
String regex = "(?<!black )cat";
String input = "The black cat and the white cat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
The ouput:
Regex: (?<!black )cat
Input: The black cat and the white cat.
Found 'cat' from 28 to 31