Regex gets NUMBER from String only

I get "7+" or "5+" or "+5" from XML and want to extract only a number from a string using Regex. for example, the function Regex.Match ()

stringThatHaveCharacters = stringThatHaveCharacters.Trim(); Match m = Regex.Match(stringThatHaveCharacters, "WHAT I USE HERE"); int number = Convert.ToInt32(m.Value); return number; 
+48
c # regex
Jan 25 '11 at 10:16
source share
3 answers

\d+

\d represents any digit, + for one or more. If you want to dial negative numbers as well, you can use -?\d+ .

Please note that as a string it should be represented in C # as "\\d+" or @"\d+"

+61
Jan 25 '11 at 10:17
source share
— -

The answers above are wonderful. If you need to parse all numbers from a string carrying a sequence, then some help may be provided:

 string input = "1-205-330-2342"; string result = Regex.Replace(input, @"[^\d]", ""); Console.WriteLine(result); // >> 12053302342 
+77
Jun 22 '12 at 17:27
source share

Either [0-9] or \d 1 should be enough if you need only one digit. Add + if you need more.




1 The semantics are slightly different since \d potentially matches any decimal digit in any script that uses a decimal digit.

+4
Jan 25 '11 at 10:17
source share



All Articles