简单正则表达式问题,回答后马上给分 String[] strs=Regex.Split(("aaaa bbbbb CCCCC","[ ]+"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [a-zA-Z]+[ ]+这两种写法都不对,我刚测试过了 string pattern=@"^\S*\s*\S*\s*\S*\z";我肯定是这句话的问题。请大家就帮我改这句! string s = "aaaa bbbbb CCCCC";Regex r= new Regex(" +");string[] ss= r.Split(s);Response.Write(ss[1]+","+ss[2]+","+ss[3]); [a-zA-Z]+[ ]+这两种写法都不对,我刚测试过了-----------------------------------贴出你测试的代码 string pattern=@"^\S*\s*\S*\s*\S*\z";我肯定是这句话的问题。请大家就帮我改这句-------------------------如果你要match一个字符串的话,就这样写:Regex.Match(" cde fgh","[^ ]+").Value To: sdd330(小飞羊)我知道你这种做法,能行!但是我想知道我现在问的这种方法的解决办法! 我把代码贴出来:private void UserAlter() { Regex regex=new Regex ("\r\n"); string[] lines=regex.Split (this.allusers.Text); string pattern=@"\S*\s*\S*\s*\S* "; regex=new Regex (pattern,RegexOptions.IgnoreCase); string[,] userdata=new string [lines.Length,3]; int sequence=0; string s=null; bool wrong=false; foreach(string line in lines) { if(line.Trim ().Length ==0) //空白行 { s+="\r\n"; continue; } Match match=regex.Match (line); if(match.Success ) { string menpai = match.Groups [1].Value ; string username = match.Groups [2].Value ; string dianhua = match.Groups [3].Value ; userdata[sequence,0]=menpai; userdata[sequence,1]=username; userdata[sequence,2]=dianhua; sequence++; s+=menpai+" "+username+" "+dianhua+"\r\n"; continue; } s+=line+"//此行书写错误\r\n"; wrong=true; } this.allusers.Text=s; if(wrong) {this.Response.Write("<script>alert('格式出错啦,请检查后再试!')</script>");return;} } 其中这里的line就是我说的"aaaa bbbbb CCCCC"!! string pattern=@"^\S+\s+\S+\s+\S+$";Regex regex =new Regex(pattern,RegexOptions.IgnoreCase); Match match= regex.Match ("aaaa bbbbb CCCCC");if(match.Success ) {Response.Write("OK!");}^-^ 啊,我写的有问题,不能吧,我测试没问题啊string pattern=@"[a-zA-Z]+";string str="aaaa bbbbb CCCCCC";Regex regex=new Regex(pattern);MatchCollection match=regex.Matches(str);int count=match.Count;for(int i=0;i<count;i++) Console.WriteLine(match[i]);要不楼主再试试?:) 谢谢您看的起我,试试这个Regex r=new Regex(@"(?<text>\S+)",RegexOptions.Compiled);for (Match m = r.Match(TextBox1.Text); m.Success; m = m.NextMatch()) { string getstr=m.Result("${text}"); Response.Write(getstr +"<br>");} To:小笨哎,你到底测试过没有哦?count=0啊 我叫小笨,我就是笨啊XP HOME+VS.NET 2003下运行static void Main() { string pattern=@"[a-zA-Z]+"; string str="aaaa bbbbb CCCCCC"; Regex regex=new Regex(pattern); MatchCollection match=regex.Matches(str); int count=match.Count; Console.WriteLine(count.ToString()); for(int i=0;i<count;i++) Console.WriteLine(match[i]); Application.Run(new Form1()); }output:“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll”,未加载符号。“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll”,未加载符号。3aaaabbbbbCCCCCC“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll”,未加载符号。程序“[2772] CSDNRubbish.exe”已退出,返回值为 0 (0x0)。 此代码中的对象在堆或者栈或者内存中如何引用 的? 谁知道UInt64怎么转换为二进制 馬上給分,繁體字轉換為簡體字問題? 使用.net访问usb实现对手机数据的读取 winform的一些简单问题? 更新触发器如何写?? 帮我看看这个程序还差点什么? C# QQ交流群,大家共同学习 WEB网页能获取客户端机器哪些硬件信息 ASP.NET有没有VFP的宏代换功能????????? 新手提问,谢谢了!! 怎样使得程序运行时窗口在屏幕正中间?
[ ]+这两种写法都不对,我刚测试过了
我肯定是这句话的问题。请大家就帮我改这句!
Regex r= new Regex(" +");
string[] ss= r.Split(s);
Response.Write(ss[1]+","+ss[2]+","+ss[3]);
[ ]+这两种写法都不对,我刚测试过了
-----------------------------------
贴出你测试的代码
我肯定是这句话的问题。请大家就帮我改这句
-------------------------
如果你要match一个字符串的话,就这样写:
Regex.Match(" cde fgh","[^ ]+").Value
我知道你这种做法,能行!但是我想知道我现在问的这种方法的解决办法!
private void UserAlter()
{
Regex regex=new Regex ("\r\n");
string[] lines=regex.Split (this.allusers.Text);
string pattern=@"\S*\s*\S*\s*\S* "; regex=new Regex (pattern,RegexOptions.IgnoreCase); string[,] userdata=new string [lines.Length,3];
int sequence=0;
string s=null;
bool wrong=false;
foreach(string line in lines)
{
if(line.Trim ().Length ==0) //空白行
{
s+="\r\n";
continue;
}
Match match=regex.Match (line); if(match.Success )
{
string menpai = match.Groups [1].Value ;
string username = match.Groups [2].Value ;
string dianhua = match.Groups [3].Value ;
userdata[sequence,0]=menpai;
userdata[sequence,1]=username;
userdata[sequence,2]=dianhua; sequence++;
s+=menpai+" "+username+" "+dianhua+"\r\n";
continue;
}
s+=line+"//此行书写错误\r\n";
wrong=true;
}
this.allusers.Text=s; if(wrong) {this.Response.Write("<script>alert('格式出错啦,请检查后再试!')</script>");return;}
}
Regex regex =new Regex(pattern,RegexOptions.IgnoreCase);
Match match= regex.Match ("aaaa bbbbb CCCCC");
if(match.Success )
{Response.Write("OK!");}
^-^
string str="aaaa bbbbb CCCCCC";
Regex regex=new Regex(pattern);
MatchCollection match=regex.Matches(str);
int count=match.Count;
for(int i=0;i<count;i++)
Console.WriteLine(match[i]);要不楼主再试试?:)
for (Match m = r.Match(TextBox1.Text); m.Success; m = m.NextMatch())
{
string getstr=m.Result("${text}");
Response.Write(getstr +"<br>");
}
哎,你到底测试过没有哦?count=0啊
XP HOME+VS.NET 2003下运行static void Main()
{
string pattern=@"[a-zA-Z]+";
string str="aaaa bbbbb CCCCCC";
Regex regex=new Regex(pattern);
MatchCollection match=regex.Matches(str);
int count=match.Count;
Console.WriteLine(count.ToString());
for(int i=0;i<count;i++)
Console.WriteLine(match[i]);
Application.Run(new Form1());
}
output:
“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll”,未加载符号。
“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll”,未加载符号。
3
aaaa
bbbbb
CCCCCC
“CSDNRubbish.exe”: 已加载“c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll”,未加载符号。
程序“[2772] CSDNRubbish.exe”已退出,返回值为 0 (0x0)。