Append a Quantifier
00:00
Since timestamps are always the same length, you can use a handy trick here and add a quantifier to your regular expression. Quantifiers are set in curly braces ({}
).
00:13 The moment you add a number into the quantifier, the regular expression looks for the pattern that you had before exactly one time. So that’s not different than what you did before.
00:25
The moment you add a 2
into it, something’s changed, and it almost seems that you are getting the full timestamp. However, the 0
at the end is not matched. And if you look closely, the visualizer also shows you two different colors.
00:41
One is a bit of a lighter blue, and the other one is a bit of a darker blue, and that means the regular expression matches two found tokens from the expression before. So it’s like the 20
from 2022
and then the 22
and then the -0
, and then the 8-
, and so on.
00:59
So it always matches a pair of two. So what you can do now is count the characters that the timestamp contains, and that’s 25. So once you put in 25
here, then you can see that your full timestamp is matched.
01:16 Now the match of the timestamp has the same color, so that means the entity of the timestamp matches your regular expression. Or to be more precise, that your regular expression now matches the timestamp that you have in your test string.
01:31 You can even make one more adjustment to this regular expression, and that’s putting a space on the front of your regular expression because that way, you will also match the space before the timestamp.
01:44
The 25
quantifier stays the same because it only refers to the token that is found before, and that’s your character set that you’re looking for in timestamp.
01:53 And the space is literally the space that your regular expression matches now. You can see the space that I’m talking about visualized as this dot between the username and the timestamp.
02:06 So this will be helpful when you work with the regular expression in the next lesson. Just like before, copy this regular expression and don’t forget the space at the beginning and paste it into a text file.
Become a Member to join the conversation.