regex for matching words starting with special chars

45 views Asked by At

Im trying to match #TEST in a string.. lets say its "this is a #TEST"

The script that does the matching automatically wraps the search string in \b tags so the resulting regex would look like this: \b#TEST\b .. this obviously doesn't work.

How can i change the search string to make it work ?

EDIT: i opted to change the wrapping function so the generated expression looks like this:

(?<!\S)#TEST(?!\S)

edit2: escaping my text..

3

There are 3 answers

0
Justin Vandehey On

The '\' character is escaping the 'b' character. Replace '\b' with '\\b', assuming your function is literally wrapping the search string with '\b'.

So: '\\b#TEST\\b'

4
anubhava On

You can probably use:

(^|\W)TEST\b

RegEx Demo

i.e. match line start or a non-word character before TEST on left hand side.

0
AudioBubble On

You could sort of overcome that by asserting a #, but it won't be part of the match.
However, it won't match anything else either.

\b(?<=#)TEST\b

So pass in (?<=#)TEST