Well, you could also build this parser with a recursive regex:
$regex = "([a-zA-Z0-9.-]+|\"([^\"\\\\]+(?1)|\\\\.(?1)|)\"|'([^'\\\\]+(?2)|\\\\.(?2)|)')s";
Now that's a bit long, so let's break it out:
$identifier = '[a-zA-Z0-9.-]+';
$doubleQuotedString = "\"([^\"\\\\]+(?1)|\\\\.(?1)|)\"";
$singleQuotedString = "'([^'\\\\]+(?2)|\\\\.(?2)|)'";
$regex = "($identifier|$doubleQuotedString|$singleQuotedString)s";
So how does this work? Well, the identifier should be obvious...
The two quoted sub-patterns are basically, the same, so let's look at the single quoted string:
'([^'\\\\]+(?2)|\\\\.(?2)|)'
Really, that's a quote character followed by a recursive sub-pattern, followed by a end quote.
The magic happens in the sub-pattern.
[^'\\\\]+(?2)
That part basically consumes any non-quote and non-escape character. We don't care about them, so eat them up. Then, if we encounter either a quote or a backslash, trigger an attempt to match the entire sub-pattern again.
\\\\.(?2)
If we can consume a backslash, then consume the next character (without caring what it is), and recurse again.
Finally, we have an empty component (if the escaped character is last, or if there's no escape character).
Running this on the test input @HamZa provided returns the same result:
array(8) {
[0]=>
string(3) "foo"
[1]=>
string(13) ""bar \"baz\"""
[2]=>
string(10) "'\'quux\''"
[3]=>
string(9) "'foo"bar'"
[4]=>
string(9) ""baz'boz""
[5]=>
string(5) "hello"
[6]=>
string(16) ""regex
world\"""
[7]=>
string(18) ""escaped escape\\""
}
The main difference that happens is in terms of efficiency. This pattern should backtrack less (since it's a recursive pattern, there should be next to no backtracking for a well-formed string), where the other regex is a non-recursive regex and will backtrack every single character (that's what the ?
after the *
forces, non-greedy pattern consumption).
For short inputs this doesn't matter. The test case provided, they run within a few % of each other (margin of error is greater than the difference). But with a single long string with no escape sequences:
"with a really long escape sequence match that will force a large backtrack loop"
The difference is significant (100 runs):
- Recursive:
float(0.00030398368835449)
- Backtracking:
float(0.00055909156799316)
Of course, we can partially lose this advantage with a lot of escape sequences:
"This is \" A long string \" With a\lot \of \"escape \sequences"
- Recursive:
float(0.00040411949157715)
- Backtracking:
float(0.00045490264892578)
But note that the length still dominates. That's because the backtracker scales at O(n^2)
, where the recursive solution scales at O(n)
. However, since the recursive pattern always needs to recurse at least once, it's slower than the backtracking solution on short strings:
"1"
- Recursive:
float(0.0002598762512207)
- Backtracking:
float(0.00017595291137695)
The tradeoff appears to happen around 15 characters... But both are fast enough that it won't make a difference unless you're parsing several KB or MB of data... But it's worth discussing...
On sane inputs, it won't make a significant difference. But if you're matching more than a few hundred bytes, it may start to add up significantly...
Edit
If you need to handle arbitrary "bare words" (unquoted strings), then you can change the original regex to:
$regex = "([^\s'\"]\S*|\"([^\"\\\\]+(?1)|\\\\.(?1)|)\"|'([^'\\\\]+(?2)|\\\\.(?2)|)')s";
However, it really depends on your grammar and what you consider a command or not. I'd suggest formalizing the grammar you expect...