Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Work With a Token

00:00 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?

00:25 So you don’t want to look specifically for Tom, but basically for any support agent.

00:32 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,

00:50 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.

01:06 So, the next thing that you need to do is to put an asterisk (*).

01:11 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.

01:53 That’s cool. Okay, but let’s leave it with support_tom and copy the regular expression input and save it in text file because you will need it in a moment.

Become a Member to join the conversation.