求c# 用正则来获取标签里的属性列表

WorldFocus 2009-02-03 10:27:18
例如下面的标签
<region name=newslist col=4 row=10 order=desc>abcsadf </region>

我要去的这个 region 里的所有属性列表
name
col
row
order
已经分别的值

属性是随机的,需要获得属性的列表,谢谢,请大家帮忙
...全文
203 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
guttersnipe_8 2009-02-04
  • 打赏
  • 举报
回复
ding
-过客- 2009-02-03
  • 打赏
  • 举报
回复
嗯,上面写的有个小bug,最后的属性值会把“>”取进来,再有就是按上面的方法,第二个正则可以简化一下,

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, @"(([^\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";
}
}
路人乙e 2009-02-03
  • 打赏
  • 举报
回复
这么规矩?
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"}...
WorldFocus 2009-02-03
  • 打赏
  • 举报
回复
感谢朋友了,没话说!!!!
-过客- 2009-02-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 WorldFocus 的回复:]
1、源字符串中有多个这样的 <region> </region>标签
2、每个 <region...>标签分开取
3、属性对应的值也要得到的
4、属性的=后全部是直接接非空格字符串,非常干净
[/Quote]

常规写法

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";
}
}
WorldFocus 2009-02-03
  • 打赏
  • 举报
回复
1、源字符串中有多个这样的 <region></region>标签
2、每个 <region...>标签分开取
3、属性对应的值也要得到的
4、属性的=后全部是直接接非空格字符串,非常干净
WorldFocus 2009-02-03
  • 打赏
  • 举报
回复
1、源字符串中有多个这样的 <region></region>标签
2、每个 <region...>标签分开取
3、属性对应的值也要得到的
4、属性的=后全部是直接接非空格字符串,非常干净
-过客- 2009-02-03
  • 打赏
  • 举报
回复
常规方法,同样需要楼主把规则明确一下,如有必要,根据规则修改一下正则即可

string test = "<region name=newslist col=4 row=10 order=desc>abcsadf </region> ";
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、属性的=后全部是直接接非空格字符串,还是有可能出现"...",'...'的情况
WorldFocus 2009-02-03
  • 打赏
  • 举报
回复
正则书写我是新手,楼上的试了,可以得到
name
col
row
order

我还希望得到他们各自的值呢,谢谢,标签书写很规则,不需要考虑特殊情况,还请朋友完善一些正则
-过客- 2009-02-03
  • 打赏
  • 举报
回复
先来个非常规的,如果=后可能会接"..."或'...',说明一下,正则需要修改一下

List<string> list = new List<string>();
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";
}

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧