Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex comparison

Tags:

regex

I'm (finally) starting to learn regex, and I'm wondering if there's any notable difference between these two pattern strings. I'm trying to match lines such as "Title=Blah", and match "Title" and "Blah" in two groups.

The problem comes with titles like "Title=The = operator". Here are the two choices to solve the problem:

^([^=]+)=(.+)$
^(.+?)=(.+)$

Is there any difference between the two, either performance-wise or functionality-wise?

like image 989
zildjohn01 Avatar asked Dec 30 '22 18:12

zildjohn01


1 Answers

The first one requires that there be at least one non-= character before an = in order to match, where the second doesn't; it'll match on a leading ==.

As to performance, I don't anticipate a meaningful difference, but if you truly care, the only thing to do is profile it. Which I would do by writing a pair of scripts, each running one of the methods a few hundred thousand times, and timing them using the Unix time command.

like image 141
chaos Avatar answered Feb 01 '23 07:02

chaos