使用regex c#进行字符串拆分 [英] string split using regex c#

查看:98
本文介绍了使用regex c#进行字符串拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

INPUT:

string s =([Az] {1,}) - ([az] {1,})





输出:

应该产生一个包含以下内容的数组:

([Az] {1,}}

([az] {1,})

解决方案

http://msdn.microsoft.com/en-us/library/8yttk7sy(v = vs.110).aspx [ ^ ]


最近,我发现了命名群组:



  string  []结果; 
string pattern = ((?< ; FirstGroupName> [A-Za-z] +) - ((?< SecondGroupName> [A-Za-z] +));
匹配匹配= Regex.Match( YourInputString,pattern);
if (match.Success)
{
result = new string [] {
match.Groups [ FirstGroupName],
match.Groups [< span class =code-string> SecondGroupName]
};
}


INPUT:
string s="([A-z]{1,})-([a-z]{1,})"


OUTPUT:
should result in an array containing:
([A-z]{1,})
([a-z]{1,})

解决方案

http://msdn.microsoft.com/en-us/library/8yttk7sy(v=vs.110).aspx[^]


Lately, I discovered Named Groups:

string[] result;
string pattern = "((?<FirstGroupName>[A-Za-z]+)-((?<SecondGroupName>[A-Za-z]+))";
Match match = Regex.Match("YourInputString", pattern);
if(match.Success)
{
    result = new string[]{
        match.Groups["FirstGroupName"],
        match.Groups["SecondGroupName"]
    };
}


这篇关于使用regex c#进行字符串拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆