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 = ?

1
  • 1
    try [is|=] instead of just is. Commented Jul 27, 2017 at 9:11

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Thank for the explanation :). Another thing, what it mean by Group 1 in (\d+) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.