• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1480
  • Last Modified:

Regex.Replace all non-numbers?

I have a pattern to find certain predictable characters, however I would like to replace ALL non-number characters to be safe.

string pattern = "[A-Za-z:-]";
return Regex.Replace(input, pattern, " ");
0
kvnsdr
Asked:
kvnsdr
  • 3
1 Solution
 
Bob LearnedCommented:
Try this instead:

string pattern = "[^\d]";
return Regex.Replace(input, pattern, " ");

Bob
0
 
Fernando SotoCommented:
Hi kvnsdr;

This is not for point. Please give the points to Bob.

The pattern will work just fine. But to make the Regex engine more efficient add a + at the end of the pattern.

string pattern = "[^\d]+";

Fernando
0
 
Bob LearnedCommented:
The Regex engine can be inefficient depending on the pattern.  Would adding the '+' make it better or worse?  Hmmm....

Bob
0
 
kvnsdrAuthor Commented:
Interpretation: Beginning, Any non-digit, any number of times?
0
 
Bob LearnedCommented:
Intrepretation:  Anything that is not a digit, any number of times.  The ^ operator inside of a character class is the NOT operator.  The '+' operator is the repeater

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now