正则表达式选择引号之外的字符 [英] Regex to pick characters outside of pair of quotes
问题描述
我想找到一个正则表达式,它可以挑选出引号集之外的所有逗号.
I would like to find a regex that will pick out all commas that fall outside quote sets.
例如:
'foo' => 'bar',
'foofoo' => 'bar,bar'
这将选出第 1 行 'bar',
我不太关心单引号和双引号.
I don't really care about single vs double quotes.
有没有人有任何想法?我觉得这应该可以通过预读来实现,但是我的正则表达式 fu 太弱了.
Has anyone got any thoughts? I feel like this should be possible with readaheads, but my regex fu is too weak.
推荐答案
这将匹配任何字符串,直到并包括第一个未加引号的,".这就是你想要的吗?
This will match any string up to and including the first non-quoted ",". Is that what you are wanting?
/^([^"]|"[^"]*")*?(,)/
如果你想要所有这些(并且作为对那个说不可能的人的反例)你可以写:
If you want all of them (and as a counter-example to the guy who said it wasn't possible) you could write:
/(,)(?=(?:[^"]|"[^"]*")*$)/
将匹配所有这些.于是
which will match all of them. Thus
'test, a "comma,", bob, ",sam,",here'.gsub(/(,)(?=(?:[^"]|"[^"]*")*$)/,';')
用分号替换引号内的所有非逗号,并产生:
replaces all the commas not inside quotes with semicolons, and produces:
'test; a "comma,"; bob; ",sam,";here'
如果您需要它跨换行符,只需添加 m(多行)标志.
If you need it to work across line breaks just add the m (multiline) flag.
这篇关于正则表达式选择引号之外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!