E
E
estry2020-10-07 21:14:43
Regular Expressions
estry, 2020-10-07 21:14:43

How to parse all digits from a C# string?

How to parse all digits from a string using only a regular expression (preferably without a loop)?
I tried like this, but it doesn't work.

string line = "12sds52124d22";
string result = new Regex("\\d+").Match(line).Value;

I get 12, but I need all the numbers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-10-07
@estry

It would be easier to do this:

string line = "12sds52124d22";

string result = Regex.Replace(line, @"[^\d]", "");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question