none
RegEx for a sequence with OR RRS feed

  • Question

  • Hi All,

    I'm trying to create a Regex for a string.  

    01-03 18:54:40.97 111111111---1-1--- 

    The section that contains, 111111111---1-1--- , can only be a "1", "-", or a "T",

    I know that I can type out [1|-|T] 18 times in a row to get my match, but I am wondering if there is a way to check those by something like [1|-|T]{18}.  < That does not work.

    Here was my valiant attempt:

    \d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{2}\s(?R)[1|-|T]{18}\s

    Any insight?

    Tuesday, February 20, 2018 10:29 PM

Answers

  • Try the following:

                string input = "01-03 18:54:40.97 111111111---1-1--- ";
                Regex r = new Regex("\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}.\\d{2}\\s(?<R>[1T-]{18})\\s");
                Match m = r.Match(input);
                Console.WriteLine(m.Groups["R"].Value);

    • Marked as answer by 7VN Wednesday, February 21, 2018 2:08 PM
    Wednesday, February 21, 2018 6:18 AM
    Answerer

All replies

  • Try this expression:

       \d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{2}\s(1|-|T){18}

    If required, add ‘^’ and ‘$’.

    Wednesday, February 21, 2018 6:15 AM
  • Try the following:

                string input = "01-03 18:54:40.97 111111111---1-1--- ";
                Regex r = new Regex("\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}.\\d{2}\\s(?<R>[1T-]{18})\\s");
                Match m = r.Match(input);
                Console.WriteLine(m.Groups["R"].Value);

    • Marked as answer by 7VN Wednesday, February 21, 2018 2:08 PM
    Wednesday, February 21, 2018 6:18 AM
    Answerer