Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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 - Fatal编程技术网

Regex 检索字符串中的特定文本

Regex 检索字符串中的特定文本,regex,c#-4.0,Regex,C# 4.0,我想要一个在c#脚本中检索字符串中的文本的解决方案 文本的格式是4位,然后是u和1到2位 test_p_2008_1_Annexe_1_prix test_p_2008_100_Annexe_1_prix test_p_2008_1 test_p_2008_100 对于这4个示例,我需要 2008_1 2008_100 2008_1 2008_100 也许用一个正则表达式,我不太擅长这个,我想你是想检索4位数字和1到3位格式的文本 @"\d{4}_\d{1,3}" 代码: 或使用@“\d{

我想要一个在c#脚本中检索字符串中的文本的解决方案 文本的格式是4位,然后是u和1到2位

test_p_2008_1_Annexe_1_prix
test_p_2008_100_Annexe_1_prix
test_p_2008_1
test_p_2008_100
对于这4个示例,我需要

2008_1
2008_100
2008_1
2008_100

也许用一个正则表达式,我不太擅长这个,我想你是想检索4位数字和1到3位格式的文本

@"\d{4}_\d{1,3}"
代码:

或使用
@“\d{4}\ud+”
匹配
符号后面的所有数字。
String input = @"test_p_2008_1_Annexe_1_prix
test_p_2008_100_Annexe_1_prix
test_p_2008_1
test_p_2008_100";
Regex rgx = new Regex(@"\d{4}_\d{1,3}");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);