Regex: Retreive numbers out of string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

How would I retreive only the numbers out of a string?
Regex.Replace(mString, -Expression-, "")

eg: mString = "AZE|@#{1^2#{[^3ert/.?" should return '123'

TIA,

Michael
 
Hello,

How would I retreive only the numbers out of a string?
Regex.Replace(mString, -Expression-, "")

eg: mString = "AZE|@#{1^2#{[^3ert/.?" should return '123'


You can use [0-9]+ and loop through all the matches in the collection to
the rebuild the number.
 
Michael,
Define your expression so it matches on all not numeric characters. The
standard character class for digits is "\d", the standard character class
for non-digits is "\D".

Something like:

mString = "AZE|@#{1^2#{[^3ert/.?"
mString = Regex.Replace(mString, "\D", "")


A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 
the standard character class for non-digits is "\D".

Thank you Jay, I've been looking for "that one" parameter for over an hour -
obviously in the wrong places (also at http://www.regexlib.com but could find
it there neighter).

Sure helped,

Michael
 
You can use [0-9]+ and loop through all the matches in the collection
to the rebuild the number.

Hi Anon, the thought had crossed my mind, but then I could have used the
String-Class.

Thanks, Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top