我无法理解以下正则表达式是如何工作的。
,(?=([^\"]*\"[^\"]*\")*[^\"]*$)
该表达式基本上匹配引号括起来的所有逗号 NOT 。
例如:
apple, banana, pineapple, "tropical fruits like mango, guava, key lime", peaches
将分成:
apple
banana
pineapple
"tropical fruits like mango, guava, key lime"
peaches
有人能为我提供一个很好的表达方式吗?我不明白前瞻性如何有效。
答案 0 :(得分:1)
根据RegexBuddy:
,(?=([^\"]*\"[^\"]*\")*[^\"]*$)
Match the character "," literally «,»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=([^\"]*\"[^\"]*\")*[^\"]*$)»
Match the regular expression below and capture its match into backreference number 1 «([^\"]*\"[^\"]*\")*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «*»
Match any character that is not a "A " character" «[^\"]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character """ literally «\"»
Match any character that is not a "A " character" «[^\"]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character """ literally «\"»
Match any character that is not a "A " character" «[^\"]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
(我不以任何方式与RegexBuddy或其作者有任何关系。只是软件产品的用户。)
答案 1 :(得分:1)
Look-around assertions(包括正前瞻)是零宽度检查。他们实际上并没有从输入中消耗任何东西,但如果他们不满意,他们会让正则表达式引擎回溯。
正向前瞻记住输入中的位置并尝试从当前位置向右匹配。如果它不匹配,正则表达式引擎会回溯,否则它将返回到输入中的记忆位置并在预测之后继续。
此正则表达式使用逗号并确保其余输入与([^\"]*\"[^\"]*\")*[^\"]*$
匹配。
[^\"]
表示“一个字符,而不是双引号”。*
表示前一个字符可以重复零次或多次。*
应用于此组时,它表示“包含偶数个双引号的任何字符串,以一个结尾”。[^\"]*
以提供非双引号字符的可能性。$
匹配字符串的结尾。所以总的来说,前瞻检查是否有偶数个双引号,直到逗号后面的字符串为止。
答案 2 :(得分:1)
如果您可以透视代表您的正则表达式,那么您将获得(感谢RegExpr)
您可以使用^(([^'",]+|'[^']*'|"[^"]*")|,)+$
第二个捕获组获取每个元素
注意:我不知道你使用的编程语言是什么......这使得提供一个好例子变得更加困难。因为我不知道你想要匹配什么。如果编程lanuaguage能够将每个#2组存储在一个数组中,那么就有了解决方案...