2

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 ;
        }
yossi
  • 12,945
  • 28
  • 84
  • 110

2 Answers2

6

"[^"\\\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"
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • 1
    @jaywayco, Post non matched examples – Kirill Polishchuk Jul 07 '11 at 08:25
  • it should match this blah\"blah and not this blah"blah – jaywayco Jul 07 '11 at 08:31
  • @polishchuk, I took the liberty to add an example based on the (working!) regex. – Bart Kiers Jul 07 '11 at 08:35
  • @jaywayco, Check, it will match `"blah\"blah"` and `"blah"` from `"blah"blah"`, if you wanna to validate string input, just put `^` at start and `$` at end, e.g.: `^"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"$`, so it will match `"blah\"blah"`, but not `"blah"blah"`. – Kirill Polishchuk Jul 07 '11 at 08:35
  • for it to work in c# it need to be \"[^\"\\\r\n]*(?:\\.[^\"\\\r\n]*)*\" – yossi Jul 07 '11 at 09:15
  • 1
    @yossi, it depends on the string literal you choose. You only need to escape the `"` by adding another `"` in front of it when using the `@"..."` literal, as can be seen by running the demo. – Bart Kiers Jul 07 '11 at 09:20
2

Try this

[^"\\]*(?:\\.[^"\\]*)*
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
SMK
  • 2,098
  • 2
  • 13
  • 21