例如下面的标签
<region name=newslist col=4 row=10 order=desc>abcsadf </region>我要去的这个 region 里的所有属性列表
name
col
row
order
已经分别的值属性是随机的,需要获得属性的列表,谢谢,请大家帮忙
<region name=newslist col=4 row=10 order=desc>abcsadf </region>我要去的这个 region 里的所有属性列表
name
col
row
order
已经分别的值属性是随机的,需要获得属性的列表,谢谢,请大家帮忙
string test = "<region name=newslist col=4 row=10 order=desc>abcsadf </region> ";
Regex.Replace(test, @"(?<=<region\s+([^\s=<>]+=\S+\s*)*)[^\s=<>]+(?==)", delegate(Match m) { list.Add(m.Value); return ""; }, RegexOptions.IgnoreCase);
foreach (string s in list)
{
richTextBox2.Text += s + "\n";
}
name
col
row
order 我还希望得到他们各自的值呢,谢谢,标签书写很规则,不需要考虑特殊情况,还请朋友完善一些正则
Match m = Regex.Match(test, @"<region\s+((?<pro>[^\s=]+)=\S+\s*)+>", RegexOptions.IgnoreCase);
for(int i=0;i<m.Groups["pro"].Captures.Count;i++)
{
richTextBox2.Text += m.Groups["pro"].Captures[i].Value + "\n";
}楼主需要明确的内容:
1、源字符串中只有一个<region...>标签,还是可能有多个
2、如果有多个,是取到一起,还是每个<region...>标签分开取
3、只取属性列表,属性对应的值取不取
4、属性的=后全部是直接接非空格字符串,还是有可能出现"...",'...'的情况
2、每个 <region...>标签分开取
3、属性对应的值也要得到的
4、属性的=后全部是直接接非空格字符串,非常干净
2、每个 <region...>标签分开取
3、属性对应的值也要得到的
4、属性的=后全部是直接接非空格字符串,非常干净
常规写法string test = "<region name=oldslist col=1 row=2 order=asc>abcsadf </region> jfdsajf <region name=newslist col=4 row=10 order=desc>abcsadf </region>";
MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
int z = 0;
foreach (Match m in mc)
{
richTextBox2.Text += "第" + ++z + "个region的属性:\n";
Match mList = Regex.Match(m.Value, @"<region\s+((?<pro>[^\s=]+)=(?<vla>\S+)\s*)+>", RegexOptions.IgnoreCase);
for (int i = 0; i < mList.Groups[1].Captures.Count; i++)
{
richTextBox2.Text += "属性: " + mList.Groups["pro"].Captures[i].Value + " 值: " + mList.Groups["vla"].Captures[i].Value + "\n";
}
}
string raw = "...";//元字符串(包含多个<region>)
Regex reg = new Regex(@"<region .+?>", RegexOptions.IgnoreCase);
Match mat = reg.Match(raw);
List<string[]> list = new List<string[]>();
while(mat.Success)
{
list.Add(mat.Value.Substring(7).Trim().Split(' '));
mat = reg.Match(raw, mat.Index+mat.Length);
}
//list中元素结构:{"name=newslist","col=4"},{"name=newslist","col=4"}...
MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
int z = 0;
foreach (Match m in mc)
{
richTextBox2.Text += "第" + ++z + "个region的属性:\n";
Match mList = Regex.Match(m.Value, @"(([^\s=]+)=([^\s>]+)\s*)+", RegexOptions.IgnoreCase);
for (int i = 0; i < mList.Groups[1].Captures.Count; i++)
{
richTextBox2.Text += "属性: " + mList.Groups[2].Captures[i].Value + " 值: " + mList.Groups[3].Captures[i].Value + "\n";
}
}或者按照另一种方法来处理,效率会高些
string test = "<region name=oldslist col=1 row=2 order=asc>abcsadf </region> jfdsajf <region name=newslist col=4 row=10 order=desc>abcsadf </region>";
MatchCollection mc = Regex.Matches(test, @"<region[^>]*>", RegexOptions.IgnoreCase);
int z = 0;
foreach (Match m in mc)
{
richTextBox2.Text += "第" + ++z + "个region的属性:\n";
MatchCollection mcList = Regex.Matches(m.Value, @"([^\s=]+)=([^\s>]+)", RegexOptions.IgnoreCase);
foreach (Match mList in mcList)
{
richTextBox2.Text += "属性: " + mList.Groups[1].Value + " 值: " + mList.Groups[2].Value + "\n";
}
}