0

I need to match a phrase exactly as it is.Example:

This is a sentance with my phrase in it.My phrase is here, this is another sentance with my phrase.This is the last sentance with my phrase.

In the above example, I got the following regex to work for all cases except the third one :

Pattern pattern = Pattern.compile("(\\.?)(\\b?)"+lexicon+"\\b(?!\\.)");
Matcher matcher = pattern.matcher(text);

Can someone point out how to get the missing case working.

Rishi S
  • 353
  • 1
  • 5
  • 16
  • `"(?i)\\b"+lexicon+"\\b"` will already work for you. `(?!\.)` fails the match if it is immediately followed with a `.` – Wiktor Stribiżew Mar 03 '20 at 13:42
  • Why won't something simple like `"\\b" + lexicon + "\\b"` work? – Sweeper Mar 03 '20 at 13:42
  • Sorry for a typo ....the regex does not work for the third instance, not the last one. Also @Sweeper your choice was what i was after as well. It works in Python, but not in Java. – Rishi S Mar 03 '20 at 13:52
  • @WiktorStribiżew Thanks for the suggestion, but it does not work for the third instance above where the phrase is followed by a dot and the next sentance. – Rishi S Mar 03 '20 at 13:56
  • [All 4 matches are found](https://regex101.com/r/X6VHQ5/1). – Wiktor Stribiżew Mar 03 '20 at 13:57
  • Yes, i did notice that it works in the online tool and also in python code. But does not seem to work in the code snippet in java that I pasted in my post. – Rishi S Mar 03 '20 at 13:58
  • Ok got it working. The issue was in the clean up code that was replacing the full stop with an empty string thus merging "my phrase" with the next line. Thanks for the useful insights ! – Rishi S Mar 03 '20 at 14:20

0 Answers0