String Str="减肥的324的方式将67地方45发达"; MatchCollection mc = Regex.Matches(Str,@"\d+|\D+"); string[] mm = new string[mc.Count]; for(int i=0;i<mc.Count;i++) { mm[i] = mc[i].Value; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Program { class Program { static void Main(string[] args) { String Str = "减肥的324的方式将67地方45发达"; MatchCollection mc = Regex.Matches(Str, @"\d+|\D+"); string[] mm = new string[mc.Count]; for (int i = 0; i < mc.Count; i++) { mm[i] = mc[i].Value; Console.WriteLine(mm[i]); } } } }楼上两位仁兄的方法是正解
Regex regex = new Regex(@"\d+"); MatchCollection ms = regex.Matches(""); foreach(Match m in ms) { Console.WriteLine(m.Value.ToString()); } 或Reges.Replace("",@"\d+",",")替换分割
String Str = "减肥的324的方式将67地方45发达"; MatchCollection mc = Regex.Matches(Str, @"\d+|\D+"); string[] mm = new string[mc.Count]; for (int i = 0; i < mc.Count; i++) { mm[i] = mc[i].Value; Console.WriteLine(mm[i]); }
String Str="减肥的324的方式将67地方45发达";
MatchCollection mc = Regex.Matches(Str,@"\d+|\D+");
string[] mm = new string[mc.Count];
for(int i=0;i<mc.Count;i++)
{
mm[i] = mc[i].Value;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Program
{
class Program
{
static void Main(string[] args)
{
String Str = "减肥的324的方式将67地方45发达";
MatchCollection mc = Regex.Matches(Str, @"\d+|\D+");
string[] mm = new string[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
mm[i] = mc[i].Value;
Console.WriteLine(mm[i]);
}
}
}
}楼上两位仁兄的方法是正解
MatchCollection ms = regex.Matches("");
foreach(Match m in ms)
{
Console.WriteLine(m.Value.ToString());
}
或Reges.Replace("",@"\d+",",")替换分割
{
Regex reg = new Regex( @"(?<!^)\b(?!$)", RegexOptions.ECMAScript);
return reg.Split(InPut);
}
汗,无牙,不要乱叫哈,害得我都不敢回帖了其实这种写法在我的博客里很早就已经有了,只不过使用\b,如果楼主的源字符串中再掺杂些字母之类的,得到的就不一定是楼主想要的结果了之前倒是想玩些花样
private string[] CutStr(string InPut)
{
Regex reg = new Regex(@"(?<=(\d)|\D)(?=(?(1)\D|\d))");
return reg.Split(InPut);
}
不过这样就会把捕捉组的内容也存到结果数组中去,Split方法中又没有提供这种参数可以不捕获的//这样写又太土了
private string[] CutStr(string InPut)
{
Regex reg = new Regex(@"(?<=\d)(?=\D)|(?<=\D)(?=\d)");
return reg.Split(InPut);
}
MatchCollection mc = Regex.Matches(Str, @"\d+|\D+");
string[] mm = new string[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
mm[i] = mc[i].Value;
Console.WriteLine(mm[i]);
}