I have this regex:
^(^?)*\?(.*)$
If I understand correctly, this is the breakdown of what it does:
So what does (^?)* mean?
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).
You can use the caret symbol (^) at the start of a regular expression to indicate that a match must occur at the beginning of the searched text.
The (^?)
is simply looking for the literal character ^
. The ^
character in a regex pattern only has special meaning when used as the first character of the pattern or the first character in a grouping match []
. When used outside those 2 positions the ^
is interpreted literally meaning in looks for the ^
character in the input string
Note: Whether or not ^
outside of the first and grouping position is interpreted literally is regex engine specific. I'm not familiar enough with LUA to state which it does
Lua does not have a conventional regexp language, it has Lua patterns in its place. While they look a lot like regexp, Lua patterns are a distinct language of their own that has a simpler set of rules and most importantly lacks grouping and alternation features.
Interpreted as a Lua pattern, the example will surprising a longtime regexp user since so many details are different.
Lua patterns are described in PiL, and at a first glance are similar enough to a conventional regexp to cause confusion. The biggest differences are probably the lack of an alternation operator |
, parenthesis are only used to mark captures, quantifiers (?
, -
, +
, and *
) only apply to a character or character class, and %
is the escape character not \
. A big clue that this example was probably not written with Lua in mind is the lack of the Lua pattern quoting character %
applied to any (or ideally, all) of the non-alphanumeric characters in the pattern string, and the suspicious use of \?
which smells like a conventional regexp to match a single literal ?
.
The simple answer to the question asked is: (^?)*
is not a recommended form, and would match ^*
or *
, capturing the presence or absence of the caret. If that were the intended effect, then I would write it as (%^?)%*
to make that clearer.
To see why this is the case, let's take the pattern given and analyze it as a Lua pattern. The entire pattern is:
^(^?)*\?(.*)$
Handed to string.match()
, it would be interpreted as follows:
^
anchors the match to the beginning of the string.
(
marks the beginning of the first capture.
^
is not at the beginning of the pattern or a character class, so it matches a literal ^
character. For clarity that should likely have been written as %^
.
?
matches exactly zero or one of the previous character.
)
marks the end of the first capture.
*
is not after something that can be quantified so it matches a literal *
character. For clarity that should likely have been written as %*
.
\
in a pattern matches itself, it is not an escape character in the pattern language. However, it is an escape character in a Lua short string literal, making the following character not special to the string literal parser which in this case is moot because the ?
that follows was not special to it in any case. So if the pattern were enclosed in double or single quotes, then the \
would be absorbed by string parsing. If written in a long string (as [[^(^?)*\?(.*)$]]
, the backslash would survive the string parser, to appear in the pattern.
?
matches exactly zero or one of the previous character.
(
marks the beginning the second capture.
.
matches any character at all, effectively a synonym for the class [\000-\255]
(remember, in Lua numeric escapes are in decimal not octal as in C).
*
matches zero or more of the previous character, greedily.
)
marks the end of the second capture.
$
anchors the pattern to the end of the string.
So it matches and captures an optional ^
at the beginning of the string, followed by *
, then an optional \
which is not captured, and captures the entire rest of the string. string.match
would return two strings on success (either or both of which might be zero length), or nil
on failure.
Edit: I've fixed some typos, and corrected an error in my answer, noticed by Egor in a comment. I forgot that in patterns, special symbols loose their specialness when in a spot where it can't apply. That makes the first asterisk match a literal asterisk rather than be an error. The cascade of that falls through most of the answer.
Note that if you really want a true regexp in Lua, there are libraries available that will provide it. That said, the built-in pattern language is quite powerful. If it is not sufficient, then you might be best off adopting a full parser, and use LPeg which can do everything a regexp can and more. It even comes with a module that provides a complete regexp syntax that is translated into an LPeg grammar for execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With