Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding match between optional tokens?

Tags:

regex

perl

For the strings:

I have the current regex:

m/(handle:|chat_identifier:)(.+?)(:{2}|&)/

And I am currently using $2 in order to obtain the value I wish (in the first string [email protected] and in the second, chat0123456789).

Is there a better/faster/simpler way to solve this problem, though?

like image 413
Matoe Avatar asked Feb 19 '23 10:02

Matoe


1 Answers

Whether it's "better" or not depends on the context, but you could take this approach: split the string on ":" and take the fourth element of the resulting list. That's arguably more readable than the regex and more robust if the third field can be something other than "handle" or "chat_identifier".

I think the speed would be very similar for either approach but probably for almost any implementation in perl. I'd want to show that speed was critical for this step before worrying about it...

like image 188
gcbenison Avatar answered Feb 23 '23 17:02

gcbenison