开发者

Extract a string using regular expressions [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜