正则表达式以查找引号之间的值 [英] Regular Expression to find values between quotes

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

问题描述

我有一个正则表达式来查找引号之间的值:

I have a regular expression to find values between quotes:

([\"'])(?:\\\1|.)*?\1

这工作正常,但是,如果引号之间有双引号,则它会失败并将它们分开.例如.

This works fine, however, if there are double quotes between quote, then it fails and split them too. for example.

"value1","value2","value with "" is here","value4"

我需要这样的输出

value1
value2
value with "" is here
value4

这意味着,如果双引号出现在某处,它应该在输出中返回它.有人可以帮忙吗?

that means, if the double quote appears somewhere, it should return that in output. Can anyone help with this?

推荐答案

我的第一个想法是通过将双引号添加到您的交替中来允许双引号:

My first idea was to allow double quotes by adding them to your alternation:

([\"'])(?:\\\1|\1\1|.)*?\1

但是,由于您已将量词设为惰性,因此这仍然无法正常工作.最好明确说明引号之间不允许使用未转义的引号:

However, since you've made your quantifier lazy, this will still not quite work. Better make it explicit that unescaped quotes are not permitted between quotes:

([\"'])(?:\\\1|\1\1|(?!\1).)*\1

regex101 上查看.

说明:

([\"'])   # Match a quote, remember which kind in group 1.
(?:       # Start non-capturing group:
 \\\1     # Either match a backslash-escaped quote
|         # or
 \1\1     # a doubled quote
|         # or
 (?!\1)   # (as long as it's not a quote)
 .        # any character.
)*        # Repeat as necessary
\1        # Match a corresponding quote

这篇关于正则表达式以查找引号之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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