Best way to get all digits from a string with regex

Posted by Chris Marisic on Stack Overflow See other posts from Stack Overflow or by Chris Marisic
Published on 2010-04-14T03:36:45Z Indexed on 2010/04/14 3:43 UTC
Read the original article Hit count: 487

Filed under:
|
|

Is there any better way to get take a string such as "(123) 455-2344" and get "1234552344" from it than doing this:

var matches = Regex.Matches(input, @"[0-9]+", RegexOptions.Compiled);

return String.Join(string.Empty, matches.Cast<Match>()
                                .Select(x => x.Value).ToArray());

Perhaps a regex pattern that can do it in a single match? I couldn't seem to create one to achieve that though.

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex