0

I would like to match couple of words in a text. Have following:

if ( Pattern.matches(".*\\b" + placeSub.toLowerCase() + "\\b" + placeSup.toLowerCase() + "\\b.*", sourceText.toLowerCase()) ) {
    System.out.println( String.format("Matched %s on %s", placeSub, placeSup) );        
}

The variables placeSub, placeSup & sourceText are dynamic (runtime).

The code above doesn't work (no match). However, the following matches:

if ( Pattern.matches(".*\\b" + placeSub.toLowerCase() + "\\s" + placeSup.toLowerCase() + "\\b.*", sourceText.toLowerCase()) ) {
   System.out.println( String.format("Matched %s on %s", placeSub, placeSup) ); 
}

Why is the text able to match \\s and not \\b?

Example input:

  1. placeSub : South

  2. placeSup : Sudan

  3. sourceText : tens of thousands of people have fled fierce fighting in south sudan's northern unity state

5
  • With placeSub = South and placeSup = Sudan there can't be just a \b between the two. In the example there is, however, a space, which is why \s matches. Commented May 12, 2015 at 16:04
  • Why do you need .* there? Commented May 12, 2015 at 16:05
  • How I am supposed to match 2 words separated by by unknown characters? The case of South Sudan is just an example. It would be South-Sudan or something else. Commented May 12, 2015 at 16:08
  • @MugomaJ.Okomba If they have to be separate words: /word\b.*\bword/ Commented May 12, 2015 at 16:10
  • .* matches anything before and anything after. I could have used ^ & $ but preferred to leave the expression open. Commented May 12, 2015 at 16:11

1 Answer 1

2

You should actually use:

Pattern.matches(
   ".*?\\b" + placeSub.toLowerCase() + "\\b\\W+\\b" + placeSup.toLowerCase() + "\\b.*",
   sourceText.toLowerCase())

Which will translate into:

/.*?\bsouth\b\W+\bsudan\b.*/i

See regex demo here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.