Extract a string using regular expressions [closed]
I'm probably not using the right search term, because I keep finding
'matching\validating' string with regex (returns boolean) while I want is to extract a string from an other string.
How can I extract some part开发者_运维百科s of a string using a regex pattern?
It's matching that you are looking for. The Regex.Match
and Regex.Matches
methods use a regular expression to find one or several parts of a string, and returns a Match
or MatchCollection
with the result.
Example:
string input = "Some string with 123 numbers in it. Yeah 456!";
MatchCollection result = Regex.Matches(input, @"\d+");
foreach (Match m in result) {
Console.WriteLine(m.Value);
}
Output:
123
456
精彩评论