Reputation: 193
If I have $content
s comes from different source having different patterns, should I make one pattern that fits all and do only one preg_match
:
if (preg_match('#^(pattern_a)|(pattern_b)|(pattern_c)$#', $content, $matches) {
if ($matches[1]) {
// return sth
}
if ($matched[2]) {
// return sth
}
if ($matched[3]) {
// return sth
}
}
or should I parse them seperately:
if (preg_match('#^pattern_a$#', $content, $matches) {
// return sth
}
if (preg_match('#^pattern_b$#', $content, $matches) {
// return sth
}
if (preg_match('#^pattern_c$#', $content, $matches) {
// return sth
}
Is doing
(preg_match('#^(pattern_a)|(pattern_b)|(pattern_c)$#', $content, $matches)
slower than
(preg_match('#^pattern_a$#', $content, $matches)
?
Upvotes: 1
Views: 42
Reputation: 133919
I would say the single regex with branches is almost always faster, if the regular expression engine code is good. Well, I don't vouch for PHP's regex engine. However, some regular expression engines (not PHP's) build a table of all possible state transitions (deterministic finite automata); they need to scan each character in the string only once, and the scan is about as fast no matter how complex the regular expression is, the execution time depending only on the length of the string.
Furthermore, there is a possibility to prove all branches wrong at the same time; say your pattern is abc|def|ghi
and you meet character x
, no match can occur at that point for any of these.
Upvotes: 0
Reputation: 11714
If there is a way to do them in one regex pattern, then it is definitely faster (even though for some situations it may not be noticeable). However, I would say there are some situations where you may want to separate your validation patterns for the sake of yourself and other code reviewers.
If you have a strong understanding of regex, this may not be an issue but sometimes it's nice to separate just to see which specific validation is failing. This also allows you to report for those different scenarios.
But seriously though -- nothing is more badass than whipping out a regex that solves all your validation needs.
Upvotes: 1