Dimi Dimi - 2 months ago 9
C# Question

Regex to detect numbers only between special characters

I need Regex to detect numbers only between special characters.

Pattern

;\d+=\d+?


String
0014;5010730101000033347=4510120173?AA


My objective is to get this string

;5010730101000033347=4510120173?

Answer

The \d+? at the end of the pattern matches 1 digit, no more, due to the +? lazy quantifier matching 1 or more occurrences, but as few as necessary to return a valid match.

You may use

;\d+=\d+\?
        ^^

C# declaration:

string pattern = @";\d+=\d+\?";

See the regex demo

Details:

  • ; - a semi-colon
  • \d+ - 1 or more digits
  • = - an equal sign
  • \d+ - 1 or more digits
  • \? - a literal ? char

enter image description here