Finding 'a word' using Regular Expression

We frequently need to test where a word (rather than pattern) exists in other string or not. To illustrate more, consider the following two strings:

String1: This fact is very important to understand.
String2: port

Now  two interesting cases arise:
  • Find whether pattern "port" appears in String1: In this case the regular expression would be: String regExp=".*"+ String2 +".*"; Clearly, we don't care what comes before and after the pattern. It would be true because port pattern in there in String1 (the word important contains it)
  • Find whether a word "port" appears in String1. Regular expression in this case is : String regExp=".*\\b"+ String2 +"\\b.*"; Following Java code is used to test this:
String regExp=".*\\b"+String2+"\\b.*";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(String1);
if( m.matches())
{

}
It fails here because "port" as a word doesn't appear in String1. It just appears as a pattern. If String1="The port was far" then the pattern matches because port appears as a word.

Key role is played by the \b of regular expression which is used to find word in a string.

0 comments:

Post a Comment