Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Regex: match strings like C# @" strings

OK, I have a REALLY dumb question this time.

I want to write a regex that can match a string something like a C# @" string - that is, I want to match strings between " chars, with "" being part of the string. I guess that the (to me) mysterious lookahead or lookbehind assertions should do the trick, but I can't make them work.

The pattern @" "" (.*?) "" (?!"")  " (with the IgnorePatternWhitespace) option does NOT work ... even though both the Grouping Constructs help page - ms-help://MS.NETFrameworkSDK/cpgenref/html/cpcongroupingconstructs.htm - and Programming Perl seems to suggest it should ....

Jon Shemitz
Wednesday, January 15, 2003

Too clever is stupid.

Thanks to Kragen Sitaker (and Google Groups)

  " \" ( (?: [^\"] | \"\" ) * ) \" "

does the trick. A string between double quotes, consisting of any number of repeats of EITHER anything but " OR "".

I persistently underuse alternation, perhaps because of a bad "| is expensive" meme.

Jon Shemitz
Wednesday, January 15, 2003

*  Recent Topics

*  Fog Creek Home