开发者

String search using regular expressions

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.开发者_如何学C

\"(?![^\\\\]\")\"

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"


Try this

[^"\\]*(?:\\.[^"\\]*)*
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜