提取[]中的字符string cutText = "已知的字符串";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\[(?<str>[.]+)\]+");
System.Text.RegularExpressions.Match match = regex.Match(cutText);
if(match.Success)
{
MessageBox.Show(match.Groups["str"].Value);
}
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\[(?<str>[.]+)\]+");
System.Text.RegularExpressions.Match match = regex.Match(cutText);
if(match.Success)
{
MessageBox.Show(match.Groups["str"].Value);
}
解决方案 »
- 三元运算符怎么嵌套啊?
- Visual studio 2010能不能做静默安装包?
- 正则表达式的问题,都来拿分啊
- 马上给分,奇怪问题,BinaryWriter 写入字节数不对!
- 我想问一下,有一种程序架构是介于C/S和B/S之间的,通常的做法是Windows Form系统在程序中访问Web Form页面来完成相关模块的操作,比如招行网银系统。
- 在客户电脑上装 FrameWork 2.0的问题.
- 请做过实时监控的朋友给个思路(2)
- [STAThread]和[DllImport...]的疑问 在线等待
- 想做B/S架构的,一个文件存储网站,有什么好的架构吗?
- ASP.NET中的邮件发送接受问题
- 请问:怎么把一幅图片的一部分(圆饼形)画到窗体上?
- c#初学者,请各位帮忙
然后利用string.substring(0,match.Index)就可以获得前面的字符
string s;
Regex re = new Regex("^([^]]+)\[[^]+]\]");
while ((s = sr.ReadLine()) != null)
{}
string s;
Regex re = new Regex(@"^([^[]+)\[([^]]+)\]");
while ((s = sr.ReadLine()) != null)
{
Match m = re.Match(s);
if (m.Success)
Console.WriteLine("{0} -- {1}", m.Groups[1].Value, m.Groups[2].Value);
}
谢谢,我试试。
你的解答正是我想要的,我希望一次能把[]前面和里面的字符提取出来,现在我要处理的是一个多行的Textbox的文本,好象正则表达式有个什么Multiline 选项.怎么用它?
Match m = re.Match(s);
while (m.Success)
{
s1= m.Groups[1].Value+ m.Groups[2].Value;
m.NextMatch();
}
Regex re = new Regex(@"^([^[]+)\[([^]]+)\]");
s=this.TextBox1.Text;
MatchCollection mc;
mc=re.Matches(s);
for (int i=0;i < mc.Count;i++)
{
s1=mc[i].Groups[1].Value+":"+mc[i].Groups[2].Value;
}