Greetings, I have file with the following strings:
string.Format("{0},{1}", "Having \"Two\" On The Same Line".Localize(), "Is Tricky For regex".Localize());
my goal is to get a match set with the two strings:
- Having \”Two\” On The Same Line
- Is Tricky For regex
My current regex looks like this:
private Regex CSharpShortRegex = new Regex("\"(?<constant>[^\"]+?)\".Localize\\(\\)");
My problem is with the escaped quotes in the first line I end up stopping at the quote and I get:
- On The Same Line
- Is Tricky For This Style Too
however attempting to ignore the escaped quotes is not working out because it makes the Regex greedy and I get
- Having \”Two\” On The Same Line”.Localize(), “Is Tricky For regex”
We seem to be caught between maximum and minimum munge. Is there any hope? I have some backup plans. Can you Regex backwards? that would make it easier because I can start with the “()ezilacoL.”
EDIT:
To clarify. This is my lone edge case. Most of the time the string sits alone like:
var myString = "Hot Patootie".Localize()
This one works for me:
Tested on http://www.regexplanet.com/simple/index.html against a number of strings with various escaped quotes.
Looks like most of us who answered this one had the same rough idea, so let me explain the approach (comments after
#s):For C# you’ll need to escape the slashes twice (messy):
Mark correctly points out that this one doesn’t match escaped characters other than quotation marks. So here’s a better version:
And its slashed-up equivalent:
Works the same way, except it has a special case that if encounters a slash but it can’t match
\", it just consumes the slash and the following character and moves on.Thinking about it, it’s better to just consume two characters at every slash, which is effectively Mark’s answer so I won’t repeat it.