问题
I am trying to use regex to match a a variable length of digits after the open bracket '(' character.
I have tried
\(\d+
But that regex includes the bracket in the match. How do I exclude it?
I am using sublime text regex engine to do the match.
回答1:
You may use a positive lookbehind:
(?<=\()\d+
There is one limitation here: you can only have a pattern of known width in the lookbehind. You may use (?<=\(|\s{5})\d+
, but you cannot use (?<=\d:\s*)\d+
.
You may use \K
"match reset" operator to work around the length limit in the lookbehind since \K
just "truncates" the match at the place where it is used, all text matched by the pattern to the left of it is omitted:
\(\K\d+
[

Note that \K
is not actually a lookbehind equivalent since the text before it is consumed while lookbehinds do not consume text.
回答2:
You can use capturing groups:
/\((\d+)/
The non-escaped brackets are a regex matching group which you can extract from each match. If you have a search'n'replace system, the $1
is often used to access the groups.
来源:https://stackoverflow.com/questions/42119058/regex-match-number-after