正则表达式 - 匹配句子避免括号
Regex - Match sentence avoiding parentheses
我需要制作一个正则表达式来匹配大文本中的关键字。
大文本的示例如下:
...another sentence ending Keywords: tag 1, tag 2, tag 3 (may contain
something like this ref. 1), tag 4 and tag 5. Another sentence
has begun...
正则表达式必须提取:
Keywords: tag 1, tag 2, tag 3 (may contain
something like that ref. 1), tag 4 and tag 5
我有以下代码:
\bKeywords:[^\.]+
但问题是正则表达式没有避开括号内的文本,而是以“..ref. 1..”上的点停止。
谢谢大家!
注意:单词"tag"是一个例子,可以是任何单词。
假设不能嵌套括号:Keywords: (?:[^(.]|\([^)]*\))*
。
我匹配:
(?:[^(.]|\([^)]*\))*
* as many times as possible
(?: ) non-capturing
| either:
[^(.] a character that's not an opening paranthesis or a dot, or
\( \) inside literal parantheses
[^)]* as many characters that aren't closing parantheses as possible
如果括号 可以 嵌套,那么正则表达式不是您想要的,因为您尝试捕获的语言是 context-free.
我需要制作一个正则表达式来匹配大文本中的关键字。
大文本的示例如下:
...another sentence ending Keywords: tag 1, tag 2, tag 3 (may contain something like this ref. 1), tag 4 and tag 5. Another sentence has begun...
正则表达式必须提取:
Keywords: tag 1, tag 2, tag 3 (may contain something like that ref. 1), tag 4 and tag 5
我有以下代码:
\bKeywords:[^\.]+
但问题是正则表达式没有避开括号内的文本,而是以“..ref. 1..”上的点停止。
谢谢大家!
注意:单词"tag"是一个例子,可以是任何单词。
假设不能嵌套括号:Keywords: (?:[^(.]|\([^)]*\))*
。
我匹配:
(?:[^(.]|\([^)]*\))*
* as many times as possible
(?: ) non-capturing
| either:
[^(.] a character that's not an opening paranthesis or a dot, or
\( \) inside literal parantheses
[^)]* as many characters that aren't closing parantheses as possible
如果括号 可以 嵌套,那么正则表达式不是您想要的,因为您尝试捕获的语言是 context-free.