Alex Rivera | Logout

Using regex to extract multiple numbers from strings

Asked 2012-05-31T11:04:48.163
8

I have a string with two or more numbers. Here are a few examples:

"(1920x1080)"
" 1920 by 1080"
"16 : 9"

How can I extract separate numbers like "1920" and "1080" from it, assuming they will just be separated by one or more non-numeric character(s)?

Edit
Report

1 Answer

12

The basic regular expression would be:

[0-9]+

You will need to use the library to go over all matches and get their values.

var matches = Regex.Matches(myString, "[0-9]+");

foreach(var march in matches)
{
   // match.Value will contain one of the matches
}
answered 2012-05-31T11:07:57.223

Your Answer