问题
I want to write a regex what matches everything except words between quotes. Ex.:
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."
So I want a regex what matches the following strings:
- Lorem ipsum
- sit amet, consectetur
- elit. Nunc ultrices varius odio,
- aliquet vitae.
I only have the following expression that matches substrings inside quotes:
([\"'])(?:\\\1|.)*?\1
回答1:
This regex works:
([^"]+?)(".*?"|$)
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)
回答2:
If you are using PCRE, you may use
([\"'])(?:\\.|(?!\1)[^\\])*?\1(*SKIP)(*F)|(?:[^\\"']|\\.)+
See its demo.
Details
([\"'])(?:\\.|(?!\1)[^\\])*?\1
- a"..."
or'...'
substring with escaped quote support:([\"'])
- Group 1 (referred to with\1
): a"
or'
(?:\\.|(?!\1)[^\\])*?
- 0+ occurrences (as few as possible due to*?
being lazy) of:\\.
- an escape sequence|
- or(?!\1)[^\\]
- any char other than\
and the quote char in Group 1
\1
- Same value as in Group 1 ("
or'
)
(*SKIP)(*F)
- PCRE verbs that omit the current match and make the engine proceed to the next match from the current match end position|
- or(?:[^\\"']|\\.)+
- 1 or more occurrences of:[^\\"']
- a char other than\
,'
or"
\\.
- an escape sequence.
来源:https://stackoverflow.com/questions/45811754/regex-match-everything-except-words-between-quotes