日期:2014-05-17  浏览次数:21016 次

正则表达式,分组提取,谢谢
public const string Dat_Example = "8501151012172100711111 2000071111111 899999999 05I41714 2100100 LBD9 0000 G 490200000000000000H0363700000898600910909F8180750 5956 899900000033330010160008000000000000000000000000000101 415915101216390725000AnHG<2VS 0031 415915101216390725100AnHG<2VS 0031 415915101216390725200 0031 415915101216390725300AnHG<2VS 0031 0799EOF ";

---------------------------
 
   
 415915101216390725000AnHG<2VS 0031 【我需要的】
 415915101216390725100AnHG<2VS 0031【我需要的】
 415915101216390725200 0031【我需要的】
 415915101216390725300AnHG<2VS 0031 【我需要的】
   
--------------
按照 4159来分组提取Dat_Example常量里面的字符串,(4前面有个空格)

正则表达式方面比较生疏,谢谢各位老师





------解决方案--------------------
try...

C# code
Regex reg = new Regex(@"\b4159\S+\s+\S+");
MatchCollection mc = reg.Matches(Dat_Example);
foreach (Match m in mc)
{
    richTextBox2.Text += m.Value + "\n";
}
/*-----输出-----
415915101216390725000AnHG<2VS 0031
415915101216390725100AnHG<2VS 0031
415915101216390725200 0031
415915101216390725300AnHG<2VS 0031
*/

------解决方案--------------------
探讨
try...


C# code
Regex reg = new Regex(@"\b4159\S+\s+\S+");
MatchCollection mc = reg.Matches(Dat_Example);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
/*-----输出-----……

------解决方案--------------------
C# code

string Dat_Example = "8501151012172100711111 2000071111111 899999999 05I41714 2100100 LBD9 0000 G 490200000000000000H0363700000898600910909F8180750 5956 899900000033330010160008000000000000000000000000000101 415915101216390725000AnHG<2VS 0031 415915101216390725100AnHG<2VS 0031 415915101216390725200 0031 415915101216390725300AnHG<2VS 0031 0799EOF ";
List<string> list=new List<string>();
foreach (Match m in Regex.Matches(Dat_Example,@"\b4159[^\s]+\s+[^\s]+\b"))
{
    list.Add(m.Value);
}
list.Foreach(x=>Console.WriteLine(x));
/*
415915101216390725000AnHG<2VS 0031
415915101216390725100AnHG<2VS 0031
415915101216390725200 0031
415915101216390725300AnHG<2VS 0031
*/

------解决方案--------------------
(?i)\b[A-Z\d]{2}59[^\s]+\s+[^\s]+\b
------解决方案--------------------
string pattern=@"\b[a-zA-Z0-9]{2}59[^\s]+\s+[^\s]+\b";

这样即可