C#操作字符串 “高护32班 班主任:吴会长 (2007-2008学年第一学期)” 这个字符串我怎么样才能用C#得到“高护32班” “2007-2008” “一” 这三个字符串呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //提供的字符串特征: //班级位于行首,并后跟空白字符 System.Text.RegularExpressions.Regex regClass = new System.Text.RegularExpressions.Regex(@"^\S+", System.Text.RegularExpressions.RegexOptions.Multiline); //在校周期以“-”连接两个数字 System.Text.RegularExpressions.Regex regPeriod = new System.Text.RegularExpressions.Regex(@"\d+\-\d+", System.Text.RegularExpressions.RegexOptions.Multiline); string sToMatch = "高护32班 班主任:吴会长 (2007-2008学年第一学期)"; System.Text.RegularExpressions.Match match = regClass.Match(sToMatch); if (match != null) Console.WriteLine("班组:" + match.Value); match = regPeriod.Match(sToMatch); if (match != null) Console.WriteLine("在校周期:" + match.Value);输出:班组:高护32班在校周期:2007-2008 //提供的字符串特征: //班级位于行首,并后跟空白字符 //周期以“-”连接两个数字 //“一”字夹在“第”和“学期”中间 System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex( @"^(?<class>\w+).+?(?<period>\d+\-\d+).+第(?<sp>\w)学期", System.Text.RegularExpressions.RegexOptions.Multiline); string sToMatch = "高护32班 班主任:吴会长 (2007-2008学年第一学期)"; System.Text.RegularExpressions.Match match = reg.Match(sToMatch); if (match != null) Console.WriteLine("班组:" + match.Groups["class"].Value + "\r\n周期:" + match.Groups["period"].Value + "\r\n学期:" + match.Groups["sp"].Value);班组:高护32班周期:2007-2008学期:一 (\w+)[\s\S]*?(\(|()(\d+-\d+)[\s\S]*?第([\s\S]*?)学期(\)|))取Groups[1].Value 勉强这样,但是不靠谱(\w+)[\s\S]*?(\(|()(\d+-\d+)[\s\S]*?第([\s\S]*?)学期(\)|))取Groups[1].ValueGroups[3].ValueGroups[4].Value str.Split(' ')[0] “高护32班” str.Split(' ')[3].Substring(1,9) “2007-2008”str.Split(' ')[3].Substring(13,1) “一” 使用LayoutControl控件时屏幕出现闪烁 求设计模式电子书 C#读取文件时每一列对齐的问题? Sql server事件探查器的使用 请问大家如何使窗口一直在最前面??????????????????????????? Excel导入到access C#的IO里哪个方法可以读取下1个字符? 【跪求】1篇2w字以上的英文材料 请问高手,我该如何进阶 高分,高手请入,关于Dll打包问题 C# GDI+怎么将图片填充到多边形 各位高手,C#主要应用是哪些方面,怎样学习。
//班级位于行首,并后跟空白字符
System.Text.RegularExpressions.Regex regClass = new System.Text.RegularExpressions.Regex(@"^\S+",
System.Text.RegularExpressions.RegexOptions.Multiline);
//在校周期以“-”连接两个数字
System.Text.RegularExpressions.Regex regPeriod = new System.Text.RegularExpressions.Regex(@"\d+\-\d+",
System.Text.RegularExpressions.RegexOptions.Multiline); string sToMatch = "高护32班 班主任:吴会长 (2007-2008学年第一学期)";
System.Text.RegularExpressions.Match match = regClass.Match(sToMatch);
if (match != null)
Console.WriteLine("班组:" + match.Value);
match = regPeriod.Match(sToMatch);
if (match != null)
Console.WriteLine("在校周期:" + match.Value);输出:
班组:高护32班
在校周期:2007-2008
//班级位于行首,并后跟空白字符
//周期以“-”连接两个数字
//“一”字夹在“第”和“学期”中间
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(
@"^(?<class>\w+).+?(?<period>\d+\-\d+).+第(?<sp>\w)学期",
System.Text.RegularExpressions.RegexOptions.Multiline); string sToMatch = "高护32班 班主任:吴会长 (2007-2008学年第一学期)";
System.Text.RegularExpressions.Match match = reg.Match(sToMatch);
if (match != null)
Console.WriteLine("班组:" + match.Groups["class"].Value
+ "\r\n周期:" + match.Groups["period"].Value
+ "\r\n学期:" + match.Groups["sp"].Value);班组:高护32班
周期:2007-2008
学期:一
(\w+)[\s\S]*?(\(|()(\d+-\d+)[\s\S]*?第([\s\S]*?)学期(\)|))取Groups[1].Value
Groups[3].Value
Groups[4].Value
str.Split(' ')[3].Substring(1,9) “2007-2008”
str.Split(' ')[3].Substring(13,1) “一”