2014-10-02 14 views
-1

我想一个解决方案,在C#脚本中的字符串来检索文本 文本的fomat是4个位数,然后_和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 

也许使用正则表达式BUIT我不是这个

回答

0

足够多的好,我想你想找回案文为4位,然后和_ 1至3位数字f ORMAT。

@"\d{4}_\d{1,3}" 

代码:

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); 

IDEONE

+0

或使用'@ “\ d {4} _ \ d +”'匹配所有以下的'_'符号的位数。 – 2014-10-02 12:08:13

+0

伟大的解决方案非常感谢您的快速回复 – user1898765 2014-10-02 12:42:29