如题,[objectID=5,flag=3]hello 如何匹配这样的字符串,并取出 5,3,还有 hello
解决方案 »
- 关于Mono for android 现在是不是要激活了?
- 如何换行显示添加的textbox?
- 抽象工厂的一个疑问
- DataGridView纵向单元格合并
- winform中datagrid 是否可以变成可编辑和可分页,能不能提供点资料。谢谢
- 网页中有一个按钮,怎样单击按钮后网页不刷新
- 求助,如何使窗体中的控件跟随窗体的增大而增大,减小而减小??
- 晕倒了,明明叫“共享程序集”,却又要防止别的程序访问?是不是脑袋让驴踢了?
- 控件透明背景与容器背景不同步问题
- 输出1到100之间的数字,要求性能最高,有什么好的办法?
- 如何在透明的窗体画不透明的线条
- C/S 里有没有一个属性控制 我点击树前面的+,-时不触发nodemouseclick事件啊
if (Regex.IsMatch(input, pattern)) {
Match m = Regex.Match(input, pattern);
// 获取消息字符串实际应有的长度
length = Convert.ToInt32(m.Groups[0].Value);
}
Regex reg = new Regex(@"(?i)\[objectID=([^,]*),flag=([^\]]*)\](\S*)");
Match m = reg.Match(test);
if (m.Success)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
richTextBox2.Text += m.Groups[2].Value + "\n";
richTextBox2.Text += m.Groups[3].Value + "\n";
}
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;namespace RegexTest
{
class Program
{
static void Main(string[] args)
{
Regex reg = new Regex(@"(?<objID>(?<=objectID=)\d+)\S+(?<flag>(?<=flag=)\d+)\S+(?<str>(?<=])\S+)");
string srcStr = "[objectID=5,flag=3]hello";
string objID = reg.Match(srcStr).Groups["objID"].Value;
string flag = reg.Match(srcStr).Groups["flag"].Value;
string str = reg.Match(srcStr).Groups["str"].Value;
string desStr = objID + " " + flag + " " + str;
Console.WriteLine(desStr);
Console.Read();
}
}
}
其实主要是hello的规则,如何知道它已经结束了,或者说匹配到哪为止
string test = "[objectID=0,flag=1]hello[objectID=0,flag=1]good ";
Regex reg = new Regex(@"(?i)\[objectID=([^,]*),flag=([^\]]*)\]([^\[\]]*)");
MatchCollection mc = reg.Matches(test);
foreach(Match m in mc)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
richTextBox2.Text += m.Groups[2].Value + "\n";
richTextBox2.Text += m.Groups[3].Value + "\n";
}
后一个good 显然不是的,不过我13楼的代码应该可以满足你的需求了