开发者

Extract a string using regular expressions [closed]

开发者 https://www.devze.com 2023-04-10 18:14 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号