I want to match strings like:
The sentence is 'He said "Hello there"'
The sentence is "He said 'Hello there'"
and get back a single capture (match) that is the sentence inside the outer single or double quotes.
^The sentence is (?:(?:'([^']*)')|(?:"([^"]*)"))$
The above regex gives me back 2 captured groups, one of them empty and the other containing the desired sentence.
^The sentence is (['"])(.*)1$
Returns the quotation mark (single or double quote) as the 1st group and the sentence as the 2nd group.
If I make the first group non-capturing,
^The sentence is (?:['"])(.*)1$
then I cannot use the later reference to the captured group. (the 1 is, of course, no longer referring to the single or double quote match)
Is there a way to have groups whose "capture" can be referenced later in the regex, but whose captured value is not returned in the list of matches?
Or some other way to solve my (seemingly simple) problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…