0

I am trying to match an exact word with multiple patterns using regex.

What I tried:

pattern: "\b(@DOG|@DOG1|@DOG2)\b", text: "@DOG2".

I want to match exact text "@DOG2" using these patterns, and the expected result should be: there is only 1 match "@DOG2". How can I change the pattern text to achieve this?

Interestingly, if I remove all '@' symbol from above text, the regex will work as expected:

pattern: "\b(DOG|DOG1|DOG2)\b", text: "DOG2".

Does '@' affect the result? How can I avoid this?

Vincent w
  • 31
  • 1
  • 6
  • 1
    `\b` is a [word boundary](https://stackoverflow.com/a/1324756/5527985), I guess `@` does not belong to *word characters*. If you remove the initial `\b` [it will match (demo)](https://regex101.com/r/XFlVql/1). You could also write [`@(?:DOG|DOG1|DOG2)\b`](https://regex101.com/r/XFlVql/3) in this case. – bobble bubble Nov 30 '22 at 03:22
  • 1
    ok seems I misunderstood 'word boundary' : ) – Vincent w Nov 30 '22 at 03:31

0 Answers0