What does this regex expression in Qt mean? I can't understand the meaning behind the ?=
.
QRegularExpression functionPattern("\\b[A-Za-z_][A-Za-z0-9_]*(?=\\()");
P.S.: This is a regex expression about parsing the C language function name.
CodePudding user response:
First, remember to unescape the double backward slashes \\
into a single backslash \
, obtaining the actual regex \b[A-Za-z_][A-Za-z0-9_]*(?=\()
.
CodePudding user response:
As Nole pointed out, you should unescape the double backward slashes into a single backslash. A single backslash followed by certain characters has special meaning. E.g., \b
means the boundary of a word and it doesn't capture anything. For example, \bword\b
matches word
, something, word, something else
, but not password
. (?=…)
is a positive lookahead and it is a non-capturing group, i.e., it doesn't capture anything. It means that there should be …
in that position. In our case, (?=\()
means there should be (
in the position. Note that the single backslash before (
used to mean the literal (
and not its meaning in RegEx context, i.e., grouping.
The whole pattern means a word (and not part of a word, since we used \b
at the beginning) that Starts with a letter or an underscore ([A-Za-z_]
) that "may" followed by a letter, an underscore or a number ([A-Za-z0-9_]*
; "may" refers to the *
). And it should be followed by (
.
Note again that this pattern captures whole the word up until the (
and not (
itself.