-1

我需要下面的字符串在正则表达式中有效。

string pattern = @"({[0-9]+}) (=|>|<|\*A*) ([a-z0-9]+)";
string input = "{123} = \"10\" || {12334} < 1000 || {8} > abcs || {34} *A* 33 || {22} *A* \"ábcd\"";

Regex rgx = new Regex(pattern, RegexOptions.Compiled);
MatchCollection matches = rgx.Matches(input);

if (matches.Count > 0)
{
    Console.WriteLine("{0} ({1} matches):", input, matches.Count);
    foreach (Match match in matches)
        Console.WriteLine("   " + match.Value);
}
else
    Console.WriteLine("Nothing" );

如何使我的正则表达式适用于所有情况下的字符串(输入)?上面的代码应该返回 5 个匹配项。

4

1 回答 1

3

Try this one:

(\{[0-9]+\})\s+(\=|\>|\<|\*A\*)\s+\"?([\p{L}\d]+)\"?

You needed to escape the second * too. Also your input string contains unicode letters which do not fall into [a-z] therefore I used \p{L} instead which matches all letters. Also you didn't account for optional quotes around letters, so I added two \"? around the right-hand-side of the expression. To store the above in an @-quoted string you need to do repeat double quotes twice, as in:

string pattern = @"(\{[0-9]+\})\s+(\=|\>|\<|\*A\*)\s+\""?([\p{L}\d]+)\""?";

I have a tendency to escape all the symbols while it might not be necessary.

于 2012-11-26T23:19:07.507 回答