Reputation: 15349
I have this regex:
^(^?)*\?(.*)$
If I understand correctly, this is the breakdown of what it does:
So what does (^?)* mean?
Upvotes: 19
Views: 73024
Reputation: 43366
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.
Upvotes: 7
Reputation: 755467
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
Upvotes: 22
Reputation: 31095
It looks to me like the intent of the creator of the expression was to match any number of ^ before the question mark, but only wanted to capture the first instance of ^. However, it may not be a valid expression depending on the engine, as others have stated.
Upvotes: 1
Reputation: 2124
In this case, the (^?) refers to the previous string "^" meaning the literal character ^ as Jared has said. Check out regexlib for any further deciphering.
For all your Regex needs: http://regexlib.com/CheatSheet.aspx
Upvotes: 2