1

I need to find word ( I know it) in a text of any lenght, like the following.

My text is very beautyfull. Yours  $text is very bautyfull. Their #text, is very beautyfull

# and $ are only sample. I can have any non-alphnumerical character

I have found the following regex:

(?<=^|[^a-zA-Z0-9])\Q<word>\E(?=$|[^a-zA-Z0-9])

tath work very well if i search #text or $text, but when i search only text it match all occurrences (three in example below) instead only one occurrence text. Is there a way to do this with regex?

theShadow89
  • 1,307
  • 20
  • 44

1 Answers1

0

Add # and $ to the first negated char class which was present inside the positive lookbehind. So that it won't match the string text which was preceded by # or $ or a-z or A-Z or 0-9

(?<=^|[^a-zA-Z0-9$#])\Qtext\E(?=$|[^a-zA-Z0-9])

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274