I need the string below is valid in regex.
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" );
How do I make my regex work for all cases the string (input)?
The above code should return 5 matches.
Try this one:
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:I have a tendency to escape all the symbols while it might not be necessary.