Difference between revisions of "Pattern Matching"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 14: Line 14:
 
boolean found = false;
 
boolean found = false;
 
while(m.find()) {
 
while(m.find()) {
System.out.println("Match found at " + m.start() + "," + m.end());
+
System.out.println("Match found at " + m.start() + "," + m.end()); //Will print : Match found at 35,39
 
found = true;
 
found = true;
 
}
 
}
Line 28: Line 28:
  
 
* Thumb rule: Regex matching runs from left to right and once a source character has been consumed, it cannot be reused.
 
* Thumb rule: Regex matching runs from left to right and once a source character has been consumed, it cannot be reused.
 +
* In the below example, it will match the pattern "aba" starting at 0 and 4, but not at 2 since they are consumed during the match starting from 0.
 +
 +
<syntaxhighlight lang="java5">
 +
 +
import java.util.regex.*;
 +
 +
public class RegexTest2 {
 +
 +
public static void main(String[] args) {
 +
 +
Pattern p = Pattern.compile("aba");
 +
Matcher m = p.matcher("abababa");
 +
boolean found = false;
 +
while(m.find()) {
 +
System.out.println("Match found; starting at pos : " + m.start());
 +
found = true;
 +
}
 +
 +
if(!found) {
 +
System.out.println("No match found");
 +
}
 +
}
 +
 +
}
 +
 +
</syntaxhighlight>
  
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 09:02, 4 July 2011

  • Classes in the java.util.regex package provide regular expressions support.
  • Basic example
import java.util.regex.*;

public class RegexTest1 {

	public static void main(String[] args) {
		
		Pattern p = Pattern.compile("lazy"); //The pattern to search for
		Matcher m = p.matcher("The quick brown fox jumps over the lazy dog"); //The source against which to match the pattern
		boolean found = false;
		while(m.find()) {
			System.out.println("Match found at " + m.start() + "," + m.end()); //Will print : Match found at 35,39
			found = true;
		}
		
		if(!found) {
			System.out.println("No match found");
		}
	}

}
  • Thumb rule: Regex matching runs from left to right and once a source character has been consumed, it cannot be reused.
  • In the below example, it will match the pattern "aba" starting at 0 and 4, but not at 2 since they are consumed during the match starting from 0.
import java.util.regex.*;

public class RegexTest2 {

	public static void main(String[] args) {
		
		Pattern p = Pattern.compile("aba");
		Matcher m = p.matcher("abababa");
		boolean found = false;
		while(m.find()) {
			System.out.println("Match found; starting at pos : " + m.start());
			found = true;
		}
		
		if(!found) {
			System.out.println("No match found");
		}
	}

}