在 PCRE 中模拟未知宽度的负后视
Emulating a negative lookbehind of unknown width in PCRE
我有 following regex:
(?<=:)\s*\w+
我只想从字符串中提取 comp
或 comp
:
savedPosition: comp;
CURLSCHET.NREC ('qwertyuiop'): noprotect;
我想避免匹配像 noprotect
这样的情况,因为在所需模式之前的任何地方都有 (
或 )
。
那你应该试试这个:
[^\(\):]*:\s*(\w*)
解释:
[^\(\):]*:
捕获所有没有 ( 和 ( 和 :
:
之后是:
\s*
后跟零个或多个空白字符
\w*
后跟零或更多长度的单词
选择:
如果不想匹配前面的部分也可以试试这个解决方法:
^(?=[^\(\):]*:).*:\s*\K(\w*)
PCRE 不支持未知宽度的负后视(.NET 支持,它看起来 like this),但您可以在第一个 (
之前提取每一行的所有匹配项或 )
使用 \G
和 \K
运算符的组合,在否定字符 class [^()]
的帮助下匹配除 (
之外的任何字符和 )
.
您可以使用
(?m)(?:^|\G)[^()\n]*?:\h*\K\w+
详情
(?m)
- 开启多行模式
(?:^|\G)
- 比赛开始 string/line 或上一场比赛结束
[^()\n]*?
- 除了 (
、)
和换行符之外的任何 0+ 个字符,尽可能少
:
- 冒号
\h*
- 0+ 水平空格
\K
- 匹配重置运算符,丢弃目前匹配的所有文本
\w+
- 1 个或多个单词字符。
: matches the character : literally (case sensitive)
*
matches the character literally (case sensitive)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
\w+
matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
我有 following regex:
(?<=:)\s*\w+
我只想从字符串中提取 comp
或 comp
:
savedPosition: comp;
CURLSCHET.NREC ('qwertyuiop'): noprotect;
我想避免匹配像 noprotect
这样的情况,因为在所需模式之前的任何地方都有 (
或 )
。
那你应该试试这个:
[^\(\):]*:\s*(\w*)
解释:
[^\(\):]*:
捕获所有没有 ( 和 ( 和 ::
之后是:\s*
后跟零个或多个空白字符\w*
后跟零或更多长度的单词
选择:
如果不想匹配前面的部分也可以试试这个解决方法:
^(?=[^\(\):]*:).*:\s*\K(\w*)
PCRE 不支持未知宽度的负后视(.NET 支持,它看起来 like this),但您可以在第一个 (
之前提取每一行的所有匹配项或 )
使用 \G
和 \K
运算符的组合,在否定字符 class [^()]
的帮助下匹配除 (
之外的任何字符和 )
.
您可以使用
(?m)(?:^|\G)[^()\n]*?:\h*\K\w+
详情
(?m)
- 开启多行模式(?:^|\G)
- 比赛开始 string/line 或上一场比赛结束[^()\n]*?
- 除了(
、)
和换行符之外的任何 0+ 个字符,尽可能少:
- 冒号\h*
- 0+ 水平空格\K
- 匹配重置运算符,丢弃目前匹配的所有文本\w+
- 1 个或多个单词字符。
: matches the character : literally (case sensitive)
*
matches the character literally (case sensitive)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
\w+
matches any word character (equal to [a-zA-Z0-9_])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)