简体   繁体   English

使用正则表达式捕获用逗号分隔的字符串中带引号的字符串

[英]Capture quoted string in comma separated string using regular expression

I am trying to match all the string withing double quotes which is separated by comma For eg in the sample string 我正在尝试用双引号将所有字符串匹配,并用逗号分隔,例如在示例字符串中

"COUNT","count(1)","crmuser.accounts"

i want to match 我想搭配

COUNT
count(1)
crmuser.accounts

Regular expression (?<=").*?(?=") is matching the comma seperator as well which is not required. 正则表达式(?<=").*?(?=")也与逗号分隔符匹配,这不是必需的。 How can i exclude commas in the string. 如何排除字符串中的逗号。

There are a few different workarounds you could use for this; 您可以使用几种不同的解决方法。 for example using capture groups as suggested in the comments by The fourth bird. 例如使用第四只鸟的注释中建议的捕获组。 But for me personally, I try to avoid using .* . 但是对我个人而言,我尽量避免使用.*

For this type of example, I would honestly recommend just using a character set of valid characters you will use and looking for more than one character (2 or more) because you will effectively skip lone separators such as a single , . 对于这种类型的示例,老实说,我建议仅使用要使用的有效字符的字符集,并查找多个字符(2个或更多),因为您将有效地跳过单独的分隔符,例如单个, You can even add a comma to that character set and it will still work. 您甚至可以在该字符集中添加一个逗号,它仍然可以使用。

(?<=")[\w\(\)\.,]{2,}(?=")

Demo 演示版

For this format of string you could just use : 对于这种字符串格式,您可以使用:

var str = '"COUNT","count(1)","crmuser.accounts"' ;
var separated = str.split('","') ;
for(var i in separated){
     i.replace('"','') ;
} ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM