Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式提取字符串_Regex_C# 4.0_String Matching - Fatal编程技术网

Regex 使用正则表达式提取字符串

Regex 使用正则表达式提取字符串,regex,c#-4.0,string-matching,Regex,C# 4.0,String Matching,我可能没有使用正确的搜索词,因为我一直在查找 “匹配\验证”字符串与正则表达式(返回布尔值),而我想从其他字符串中提取一个字符串 如何使用正则表达式模式提取字符串的某些部分?这是您要查找的匹配项。Regex.Match和Regex.Matches方法使用正则表达式查找字符串的一个或多个部分,并返回带有结果的Match或MatchCollection 例如: string input = "Some string with 123 numbers in it. Yeah 456!"; Match

我可能没有使用正确的搜索词,因为我一直在查找

“匹配\验证”字符串与正则表达式(返回布尔值),而我想从其他字符串中提取一个字符串

如何使用正则表达式模式提取字符串的某些部分?

这是您要查找的匹配项。
Regex.Match
Regex.Matches
方法使用正则表达式查找字符串的一个或多个部分,并返回带有结果的
Match
MatchCollection

例如:

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);
}
输出:

123
456

你能提供更多的细节,也许是你想做什么的一个例子吗?我能用正则表达式来表示unicode字符吗(不是英语)?是的,你能。例如,
\d
代码匹配任何语言的数字,而不仅仅是阿拉伯数字(0-9)。