i am trying to match strings that dont contain quotation marks but they can contain escaped quotation marks.
when i say string i mean quotation marks and a string inside them.
i am using this regular expression but it does not work.
\"(?![^\\\\]\")\"
solution:
@"""[^""\\\r\n]*(?:\\.[^""\\\r\n]*)*"""
the code (c#)
MatchCollection matches = Regex.Matches(input,@"""[^""\\\r\n]*(?:\\.[^""\\\r\n]*)*""");
foreach (Match match in matches)
{
result += match.Index + " " + match.Value + System.Environment.NewLine ;
}
"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"
http://www.regular-expressions.info/examplesprogrammer.html
Note that you'll need to escape certain chars properly (depending on what string literal you use)! The following demo:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string input = "foo \"some \\\" text\" bar";
Match match = Regex.Match(input, @"""[^""\\\r\n]*(?:\\.[^""\\\r\n]*)*""");
if (match.Success)
{
Console.WriteLine(input);
Console.WriteLine(match.Groups[0].Value);
}
}
}
will print:
foo "some \" text" bar "some \" text"