Home > Mobile >  Regex should not be recognized for special characters
Regex should not be recognized for special characters

Time:10-19

I want the regex not to be recognized, should be a special character before, between and after the regex.

My Regex:

\b([t][\W_]*?) ([e][\W_]*?) ([s][\W_]*?) ([t][\W_]*?)*?\b

https://regex101.com/r/zKg2eR/1

Example:

#test, te st, t'est or =test etc.

I hope I could bring it across reasonably understandable.

CodePudding user response:

If you want to match a word character excluding an underscore, you can write it as [^\W_] using a negated character class.

You don't need a character class for a single char [t] and you are repeating the groups as well, which you don't have to when you want to match a form of test

If the words are on a single line, you can append anchors ^ and $

^(t[^\W_]*)(e[^\W_]*)(s[^\W_]*)(t[^\W_]*)$

Regex demo

As you selected golang in the regex tester, you can not use lookarounds. Instead you can use an alternation to match either a whitespace char or the start/end of the string.

Then capture the whole match in another capture group.

(?:^|\s)((t[^\W_]*)(e[^\W_]*)(s[^\W_]*)(t[^\W_]*))(?:$|\s)

Regex demo

  • Related