0

I'm using this regex to get just a number back.

Regex.Replace(foo, "[^.0-9]", "")

How do I make it not remove spaces?

Hoody
  • 3,361
  • 9
  • 45
  • 55

4 Answers4

8

That's easy:

Regex.Replace(foo, "[^.0-9\\s]", "")
Nuffin
  • 3,882
  • 18
  • 34
2

You may find the Regex slightly easier to read with the @"" terminology (no need to escape the backslash:

Regex.Replace(foo, @"[^.0-9\s]", "")
Barry Kaye
  • 7,682
  • 6
  • 42
  • 64
1

How about something like this:

[^(.0-9)|( )]
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0
Regex.Replace(yourString, "[^.0-9\\s]", "");
Dummy01
  • 1,985
  • 1
  • 16
  • 21