Java Pattern Matcher


Here is a piece of code which can be useful for searching for particular pattern in a string of text. SOA archiect / Java Developer using mail service(JMS).

package com.PatternMatcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternMatcher_Test {

public static void main(String[] arg){

String strSubject=”Your Airtel Bill for Account 102-102672541, Duration 11/05/2008 to 10/06/2008″;

String[] strPatterns={ “102-102672541” };

if(bTextContainsPatterns(strSubject,strPatterns)){
System.out.println(“SUCCESS:Pattern matched”);
}else{
System.err.println(“ERROR:Pattern matched”);
}
}

private static boolean bTextContainsPatterns(String strText, String[] strPatterns) {
String strList = “”;

for (int i = 0; i < strPatterns.length; i++) {
strList += (i > 0 ? “,” : “”) + strPatterns[i].toString();
}

for (int i = 0; i < strPatterns.length; i++) {
if (strText.indexOf(strPatterns[i]) >= 0) {
System.out.println(“bTextContainsPatterns… found in element : ” + i + ” at position “+ strText.indexOf(strPatterns[i]));
System.out.println(“strText: “+strText+” | found at index:: +strText.indexOf(strPatterns[i]));
return true;
}else{
System.err.println(“bTextContainsPatterns…not found in element : ” + i + ” at position “+ strText.indexOf(strPatterns[i]));
}
}
return false;
}

}

Advertisements

One thought on “Java Pattern Matcher”

  1. Hey there, I think your website might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but
    when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, wonderful blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s