首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

两个字符串,使用正则表达式来处理,大家来看看,该怎么处理

2012-03-28 
两个字符串,使用正则表达式来处理,大家来看看1、原始串?xml version1.0 encodingutf-8?Response

两个字符串,使用正则表达式来处理,大家来看看
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();                                            } 

热点排行