我有一个字符串,我想从中捕获冒号之后(包括冒号)直到(不包括)空格或括号的所有内容。
为什么以下正则表达式在字符串匹配中包含括号?
:(.*?)[\(\)\s]
或 :(.+?)[\)\s]
(非贪婪)不起作用.
示例输入:WHERE t.operator_id = :operatorID AND (t.merchant_id = :merchantID) AND t.readerApplication_id = :readerApplicationID AND t.accountType in :accountTypes
应提取:operatorID
、:merchantID
、:readerApplicationID
、:accountTypes
。
但我的正则表达式提取了第二场比赛 :marchantID)
出了什么问题以及为什么?
即使我在捕获中使用更精确的映射条件,它也不起作用:
:([a-zA-z0-9_]+?)[\)\(\s]
最佳答案
将条件“后跟空格或括号”设置为 lookahead ,这样它可以看到但不匹配。现在,您正在将括号与 [\(\)\s]
显式匹配:
:(.+?)(?=[\s\(\)])
https://regex101.com/r/im8KWF/1/
或者,使用内置的 \b
“word boundary ”,这也是一个“零宽度”断言,含义相同*:
:(.+?)\b
https://regex101.com/r/FnnzGM/3/
*正则表达式.info 中的字边界定义:
There are three different positions that qualify as word boundaries:
Before the first character in the string, if the first character is a word character. After the last character in the string, if the last character is a word character. Between two characters in the string, where one is a word character and the other is not a word character.
关于java - 正则表达式捕获包含太多内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48405177/