Home > Mobile >  How to get numbers after some particular word using regex in c#?
How to get numbers after some particular word using regex in c#?

Time:02-08

Below is my sample text

Pick Ticket 81091 Reference 109437
Issuing Date/Time  27/Jan/2022 10:19:10AM Status 35-Pick in Progress

I need to get the 6 digits number after this word "Reference". The text will be always like this "Reference ******"

CodePudding user response:

You might simply use

Reference\s (\d )$

and use the first capturing group (switch on the multiline modifier).
See a demo on regex101.com.


In C#:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"Reference\s (\d )$";
        string input = @"Pick Ticket 81091 Reference 109437
Issuing Date/Time  27/Jan/2022 10:19:10AM Status 35-Pick in Progress
";
        RegexOptions options = RegexOptions.Multiline;
        
        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}
  •  Tags:  
  • Related