2

This is my code.

        static void Main(string[] args)
        {
            string pattern = 
@"^(?<p1>.*?)(?<c0>\w+)(?<s1>.*?)$
^(?<p2>.*?)\k<c0>(?<s2>.*?)$
^\k<p1>(?<c1>\w+)\k<s1>$
^\k<p2>\k<c1>\k<s2>$";

            string text = 
@"            if (forwardRadioButton.IsChecked.Value)
                car = car.Forward(distance);
            else if (backwardRadioButton.IsChecked.Value)
                car = car.Backward(distance);
            else if (forwardLeftRadioButton.IsChecked.Value)
                car = car.ForwardLeft(distance);";

            var mc = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Console.WriteLine(mc.Count);
            Console.ReadKey();
        }

It cannot find a match. enter image description here

But if I test the regular expression and the text in a .NET Tester, it can find a match.

enter image description here

Do I miss anything in my code? How to make the pattern work?

Gqqnbig
  • 5,845
  • 10
  • 45
  • 86
  • Know very little about Regex, but I can remember that the SingleLine and MultiLine options are very non-intuitive, and were implemented inconsistently in at least one of the test programs I used at one time. – RenniePet Jun 05 '14 at 05:11
  • I've used a test program called Expresso from Ultrapico with some success. There are also on-line Regex test pages, but typically not .Net-compatible. – RenniePet Jun 05 '14 at 05:19

3 Answers3

4

The problem is with your line endings.

The inline strings you have created in your code are terminated with \r\n, whereas the regex engine expects \n in order to match $.

Just insert these lines before the matching, and it'll work:

 pattern = pattern.Replace("\r\n", "\n");
 text = text.Replace("\r\n", "\n");

This strips out the \r, and all should be well.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

try doing something like this:

string yourtext = "yourtext";
Regex yourregex = new Regex(@"put your regex pattern here" , RegexOptions.IgnoreCase | RegexOptions.Multiline);

//put Matches in Collection
MatchCollection matchesCollection = yourregex.Matches(yourtext);

//output
Console.WriteLine(matchesCollection.Count);
Elegiac
  • 361
  • 2
  • 15
-1

I probobaly solve the problem. I removed the ^ and $ in the pattern and now I have 1 match.

It seems that if the pattern itself has multiple lines, you should not put ^ and $ in middle lines.

            string pattern =
@"^(?<p1>.*?)(?<c0>\w+)(?<s1>.*?)
(?<p2>.*?)\k<c0>(?<s2>.*?)
\k<p1>(?<c1>\w+)\k<s1>
\k<p2>\k<c1>\k<s2>$";

            string text = 
@"            if (forwardRadioButton.IsChecked.Value)
                car = car.Forward(distance);
            else if (backwardRadioButton.IsChecked.Value)
                car = car.Backward(distance);
            else if (forwardLeftRadioButton.IsChecked.Value)
                car = car.ForwardLeft(distance);";

            var mc = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Console.WriteLine(mc.Count);
            Console.ReadKey();
Gqqnbig
  • 5,845
  • 10
  • 45
  • 86
  • This is a wrong solution, because when you remove `^` and `$`, you no longer match the *entire line*. If you need a *solution*, you need to do something else. – Wiktor Stribiżew Apr 28 '20 at 07:36