日期:2014-05-18  浏览次数:20655 次

两个字符串,使用正则表达式来处理,大家来看看
1、原始串

<?xml version='1.0' encoding='utf-8'?><Response><BusinessNo>1234567890</BusinessNo><MessageCode>-100</MessageCode></Response>


2、配置串

<?xml version='1.0' encoding='utf-8'?><Response><BusinessNo>{abc}</BusinessNo><MessageCode>{def}</MessageCode></Response>

------------------------------------------
3、说明
配置串中的{xxx}格式,是我需要获得的数值

大家看看如何采用正则表达式来获得

------解决方案--------------------

C# code


 static void Main(string[] args)
            {

               string str = @"<?xml version='1.0' encoding='utf-8'?><Response><BusinessNo>1234567890</BusinessNo><MessageCode>-100</MessageCode></Response>";



                Regex re = new Regex(@"<\?xml\s* version='1.0'\s*encoding='utf-8'\?><Response><BusinessNo>([^<]+)</BusinessNo><MessageCode>([^<]+)</MessageCode></Response>", RegexOptions.None);

                Match ma = re.Match(str);
  
                Console.WriteLine(ma.Groups[1].Value);  //1234567890
                Console.WriteLine(ma.Groups[2].Value);  //-100
                Console.ReadLine();
                
                
            }