EgyEast EgyEast - 1 month ago 6
C# Question

What is the C# regex format to match +- signs or space?

I've writing code using Regex format to search for numbers that has signs before it either: + , - or space like the following numbers :

(+02.00)
(-03.50)
( 00.00)


I'm using this format but i want to include space with +-

[+-]\d{2}.\d{2}


Please help , thanks.

Answer

You can use:

[+\s-]\d{2}\.\d{2}
  • Take note of \s in the first character class [...] that will match a whitespace.
  • Unescaped hyphen should be at first or last position in character class.
  • Also, you need to escape the dot otherwise it will match any character.