作者adom0710 (工作倦怠)
看板C_Sharp
标题Re: [问题] regex及点号
时间Tue Dec 7 10:05:25 2010
※ 引述《ggg888 (g8)》之铭言:
: 请问一下
: 我有一个字串为"123.456"
: 我想用regex将该字串分成123及456
: 但是却无法成功
: 请问大家问题是出在那?
: string a="123.456";
: string[] split_a;
: Regex r_regex = new Regex(".");
: split_a=r_regex.Split(a);
如果你非得用Regex的话,你可以这样做
string target = "123.456";
Regex regex = new Regex(@"(?<Group01>\d+)\.(?<Group02>\d+)");
MatchCollection matches = regex.Matches(target);
if (matches.Count > 0)
{
GroupCollection groups = matches[0].Groups;
string Num01 = groups["Group01"].Value.Trim();
string Num02 = groups["Group02"].Value.Trim();
Console.WriteLine("First Number:" + Num01);
Console.WriteLine("Second Number:" + Num02);
}
不过如果没有特殊用途的话
建议还是用split()就好了,方便又好用!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.18.50.4