Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/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
C# Regex,检测前面带有特定字符的所有字符串_C#_.net_Regex - Fatal编程技术网

C# Regex,检测前面带有特定字符的所有字符串

C# Regex,检测前面带有特定字符的所有字符串,c#,.net,regex,C#,.net,Regex,我如何在c#中检测前面有“%”或“$”的所有字符串 编辑: 例如,如果我有以下字符串: string test = "Shipments will cost $150USD , which representes a rise of %34 ." 如何使用正则表达式检测$150USD和%34?如果您的意思是查找所有单词,可以使用该正则表达式 (?<=\s?)[%$]\w+(?=\s?) 就这么简单: string s = "Shipments will cost $150USD

我如何在c#中检测前面有“%”或“$”的所有字符串

编辑:

例如,如果我有以下字符串:

string  test =   "Shipments will cost $150USD , which representes a rise of %34 ."

如何使用正则表达式检测
$150USD
%34

如果您的意思是查找所有单词,可以使用该正则表达式

(?<=\s?)[%$]\w+(?=\s?)
就这么简单:

string s = "Shipments will cost $150USD , which representes a rise of %34 .";
            var matches = Regex.Matches(s, @"(\$|%)\w+");
            for (int i = 0; i < matches.Count; i++)
            {
                Console.WriteLine(matches[i].Value);
            }
string s=“发货成本为150美元,增长了%34。”;
var matches=Regex.matches(s,@“(\$\124;%)\ w+”;
for(int i=0;i
yes例如,如果我有字符串“$150USD”,我如何用正则表达式检测它?请查看我的答案。它的simplex我必须使用哪个正则表达式函数?Regex.Match?
匹配
,因为您需要所有匹配,而不仅仅是第一个。是的,这也很好(而且更简单)。不过,您可以使用非捕获组。
string s = "Shipments will cost $150USD , which representes a rise of %34 .";
            var matches = Regex.Matches(s, @"(\$|%)\w+");
            for (int i = 0; i < matches.Count; i++)
            {
                Console.WriteLine(matches[i].Value);
            }