Jenya Kirmiza Jenya Kirmiza - 4 months ago 9
Java Question

match regex for three words search java

I have two symbol string query for search algo. And i have a string that consist of three words separated by comma. What i want is to search among these three prarms.

e.g "String, Text,Search"

if the input is "Te" the search should match, also "Str", "Se" should match.

I implemented using regex. But it works only for the first word. Note that i have a space before the second word.

stringInput="String, Text,Search";
word="St";
String pattern1=word+"\\w*,\\s\\w*,\\w";

String pattern2="\\w*,\\."+word+"\\w*,\\w";

String pattern3="\\w*,\\w*,"+word+"\\w";

Pattern patternCompiled1=Pattern.compile(pattern1);
Pattern patternCompiled2=Pattern.compile(pattern2);
Pattern patternCompiled3=Pattern.compile(pattern3);
Matcher matcher1= patternCompiled1.matcher(inputString);

Matcher matcher2= patternCompiled2.matcher(inputString);

Matcher matcher3= patternCompiled3.matcher(inputString);

if(matcher1.find() || matcher2.find() || matcher3.find()){
return true;
}


Can you help me why it doesnt work for the second and the third word?

Some clarifications

Word1, String1, String2
The first param is always only one word, The second param can be two or more words, and the third param two - it can be several words separated by space.
eg. Text, Some Text,Other Text Text it can be anything Text, Some,Other Text
also it can contain different symbols, what i want is to make search to match first letters of first word from each param.

Answer

I am not sure why you are using Regex here, this just seems like an overly complicated solution for doing if(stringInput.contains(text))

However, if you insist on using regex, your patterns are incorrect. I highly recommend you learn some more about regex:

Your first pattern: word+"\\w*,\\s\\w*,\\w" matches:

  • The string to match
  • Followed by 0 or more word characters
  • Followed by a comma
  • Followed by a single white space character
  • Followed by 0 or more word characters
  • Followed by a comma
  • Followed by a single word character

This pattern works for the given input String, however would fail if there is a space after the last comma.

The second pattern: "\\w*,\\."+word+"\\w*,\\w" matches:

  • 0 or more word characters
  • Followed by a comma
  • Followed by a literal .
  • Followed by the string to match
  • Followed by 0 or more word characters
  • Followed by a comma
  • Followed by a single word character

This will not work because you have escaped the . character \\. which means it will match a literal . which your string does not contain.

Your final pattern: "\\w*,\\w*,"+word+"\\w" matches:

  • 0 or more word characters
  • Followed by a comma
  • Followed by 0 or more word characters
  • Followed by a comma
  • Followed by the string to match
  • Followed by a single word character

This will fail because you have not accounted for white space after the commas.

Comments