2

I have a string

string str = "I am fine. How are you? You need exactly 4 pieces of sandwiches. Your ADAST Count  is  5. Okay thank you ";

What I want is, get the ADAST count value. For the above example, it is 5.

The problem here is, the is after the ADAST Count. It can be is or =. But there will the two words ADAST Count.

What I have tried is

var resultString = Regex.Match(str, @"ADAST\s+count\s+is\s+\d+", RegexOptions.IgnoreCase).Value;
var number = Regex.Match(resultString, @"\d+").Value;

How can I write the pattern which will search is or = ?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48

1 Answers1

2

You may use

ADAST\s+count\s+(?:is|=)\s+(\d+)

See the regex demo

Note that (?:is|=) is a non-capturing group (i.e. it is used to only group alternations without pushing these submatches on to the capture stack for further retrieval) and | is an alternation operator.

Details:

  • ADAST - a literal string
  • \s+ - 1 or more whitespaces
  • count - a literal string
  • \s+ - 1 or more whitespaces
    • (?:is|=) - either is or =
  • \s+ - 1 or more whitespaces
  • (\d+) - Group 1 capturing one or more digits

C#:

var m = Regex.Match(s, @"ADAST\s+count\s+(?:is|=)\s+(\d+)", RegexOptions.IgnoreCase);
if (m.Success) {
    Console.Write(m.Groups[1].Value);
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563