我想写一个正则表达式,除了引号之间的单词之外的所有内容。例如:
Lorem ipsum "dolor" sit amet, consectetur "adipiscing" elit.
Nunc ultrices varius odio, "ut accumsan nisi" aliquet vitae.
"Ut faucibus augue tortor, at aliquam purus dignissim eget."
所以我想要一个与以下字符串匹配的正则表达式:
我只有以下表达式匹配引号内的子串:
([\"'])(?:\\\1|.)*?\1
答案 0 :(得分:1)
这个正则表达式有效:
([^"]+?)(".*?"|$)
https://regex101.com/r/um9TEx/3
1st Capturing Group ([^"]+?)
Match a single character not present in the list below [^"]+?
+? Quantifier — Matches between one and unlimited times, as few times as possible, expanding as needed (lazy)
" matches the character " literally (case sensitive)
" matches the character " literally (case sensitive)
.*? matches any character (except for line terminators)
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
" matches the character " literally (case sensitive)
答案 1 :(得分:1)
如果您使用的是PCRE,则可以使用
([\"'])(?:\\.|(?!\1)[^\\])*?\1(*SKIP)(*F)|(?:[^\\"']|\\.)+
请参阅its demo。
<强>详情
([\"'])(?:\\.|(?!\1)[^\\])*?\1
- 带有转义引用支持的"..."
或'...'
子字符串:
([\"'])
- 第1组(简称\1
):"
或'
(?:\\.|(?!\1)[^\\])*?
- 0次出现(由于*?
懒惰而导致的):
\\.
- 转义序列|
- 或(?!\1)[^\\]
- 除\
以外的任何字符和第1组中的引用字符\1
- 与第1组中的值相同("
或'
)(*SKIP)(*F)
- 省略当前匹配并使引擎从当前匹配结束位置继续进行下一场比赛的PCRE动词|
- 或(?:[^\\"']|\\.)+
- 出现1次或多次:
[^\\"']
- \
,'
或"
\\.
- 转义序列。