Work With a Token
The regular expression for
support_tom could actually look very similar. When you put in
\[support_tom\], you can see that
[support_tom] with the square brackets is selected in your test string. However, this is a good point where we could be a bit more flexible and actually leverage the powers of regular expressions. What if there are multiple support agents?
For this, you can use a so-called token, and the token that you want to use is the
w. But since the
w now should have a special meaning, it’s not enough to just put a
w into it. Instead, you need to put the escape character (
\) in front of it. Again,
once you put a backslash in front of the
w, you can see regex101 marks the regular expression input string a bit different. However, the way that it’s written right now, your regular expression doesn’t find any match in the test string.
What exactly happened there? With an asterisk, you say that the token that was used before should match zero or multiple times. So you look for a string that’s in square brackets and says
support and any alphanumerical character. That could be a letter, a number, or the underscore. In your text below, there are always more characters before the closing square bracket, and that’s why when you add the asterisk, now the
[support_tom] username in square brackets matches. And again, the cool thing is even if it would be
[support_tommy], you can see that the test string is still highlighted, so your regular expression pattern still finds the username.
Become a Member to join the conversation.