Reputation: 1347
I have various strings with different information but they all share one common characteristic: they contain the username of the requester with brackets right after it. Something like: "... paul [55] ..."
I am trying to write a regex that is able to extract the word right before the [dd]
This is what I have so far: "/(?P<user>\w+)\s\[\d\d\]/"
but this only works if the string starts with the user, such as "paul [55] has logged in"
but it wouldn't work in the case, "user paul [55] has logged off"
What am I missing?
Upvotes: 1
Views: 2084
Reputation: 726809
Try using positive lookahead assertion:
(\w+)(?=\s?\[\d\d\])
This expression matches one or more word characters only if it is followed by an optional space and a double-digit decimal number enclosed in square brackets.
Upvotes: 3