需要以下正则表达式的细分 [英] Need a breakdown of the following regular expression

查看:64
本文介绍了需要以下正则表达式的细分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解以下正则表达式的工作原理.

I have some trouble understanding how the following regular expression is working.

,(?=([^\"]*\"[^\"]*\")*[^\"]*$)

表达式基本上与所有用引号括起来的 NOT 逗号匹配.

The expression basically matches all the commas that are NOT enclosed in quotes.

例如:

apple, banana, pineapple, "tropical fruits like mango, guava, key lime", peaches

将被分为:

apple
banana
pineapple
"tropical fruits like mango, guava, key lime"
peaches

有人可以为我提供良好的表达方式吗?我不明白积极的前瞻效果如何.

Can someone provide me with a good breakdown of the expression? I don't understand how positive look-ahead is working.

推荐答案

环顾断言

环顾断言(包括正向查找)是零宽度检查.他们确实不会从输入中消耗任何东西,但是如果不满意,他们会让正则表达式引擎回退.

Look-around assertions

Look-around assertions (positive look-ahead including) are zero-width checks. They really don’t consume anything from the input, but they let the regex engine backtrack if they are not satisfied.

正向向前记住输入中的位置,并尝试从当前位置向右匹配.如果不匹配,则使用正则表达式引擎回溯,否则它将返回到输入中的记忆位置,并在前瞻后继续.

Positive look-ahead remembers the position in input and tries to match from the current position to the right. If it does not match, regex engine backtracks, otherwise it returns to the remembered position in input and continues after the look-ahead.

此正则表达式使用逗号并确保其余输入匹配([^\"]*\"[^\"]*\")*[^\"]*$.

This regex consumes a comma and ensures, that the rest of input matches ([^\"]*\"[^\"]*\")*[^\"]*$.

  • [^\"]的意思是一个字符,而不是双引号".
  • *表示前一个字符可以重复零次或多次.
  • 括号组成一个组–意思是任何包含恰好两个双引号并以一个结束的字符串".
  • 在该组上应用*时,表示任何包含偶数双引号且以1结尾的字符串".
  • 说明的以一个[双引号]结尾"部分是有问题的,您不需要这样的约束.因此,您添加[^\"]*可以为非双引号字符提供可能性.
  • $匹配字符串的结尾.
  • [^\"] means "one character, not a double-quote".
  • * means the previous character can be repeated zero or more times.
  • The parentheses form a group – it means "any string containing exactly two double-quotes, ending with one".
  • When * is applied on this group, it means "any string containing even number of double-quotes, ending with one".
  • The "ending with one [double-quote]" part of description is problem, you don’t want such a constraint. So you append [^\"]* to provide possibility for non-double-quote characters.
  • $ matches the end of string.

因此,总的来说,前瞻检查在逗号后的和字符串之前,双引号是否有偶数.

So all-in-all, the look-ahead checks if there is even number of double-quotes till the and of string after the comma.

这篇关于需要以下正则表达式的细分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆