首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

正规则有关问题

2011-12-14 
正规则问题如1attributeName*2attributename*3attributename**任意多个字符,我想取attributeName的

正规则问题

1 attributeName="*"
2 attributename='*'
3 attributename=*

  *任意多个字符,我想取attributeName的值.
  取得的结果应为
1 *
2 *
3 *  
它的正规则怎么写?
 

[解决办法]
"Attribute =(.*)"
[解决办法]
今天就结吧

 string regexpress = string.Format("{0}\\s*=\\s*[\"']?(?<content>.+?)([\"']|$)", "Attribute"); //Attribute 填入你需要的属性名
 System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regexpress);

 string val = reg.Match(test1).Groups["content"].Value;
[解决办法]
^\"\w*\"|\'\w*\'|\w*$

测试一下
[解决办法]
这样就可以了

string rex = "\"+|'+";
Console.Write(regex.Replace(str, ""));
[解决办法]
string rex = "\"+ ¦'+"; 
Regex regex = new Regex(rex);
Console.Write(regex.Replace(str, ""));


把引号和分号全都换成空字符串
[解决办法]
var str="Attribute=id"
var aa=new RegExp("^Attribute=(.*)$");
var ss=str.match(aa)
[解决办法]

探讨
比如
Attribute ="ID"
Attribute = 'ID'
Attribute = id
我要取到ID

[解决办法]
引用楼主 NewUser2008 的帖子:

1 attributeName="*"
2 attributename='*'
3 attributename=*

*任意多个字符,我想取attributeName的值.
取得的结果应为
1 *
2 *
3 *
它的正规则怎么写?


[解决办法]
探讨
今天就结吧

string regexpress = string.Format("{0}\\s*=\\s*[\"']?(? <content>.+?)([\"'] ¦$)", "Attribute"); //Attribute 填入你需要的属性名
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regexpress);

string val = reg.Match(test1).Groups["content"].Value;

[解决办法]
探讨
^\"\w*\" ¦\'\w*\' ¦\w*$

测试一下

[解决办法]
相当无语。。。
这种需求要用到反向引用

C# code
string[] test = new string[] { "attributeName=\"*\"", "attributename='*'", "attributename=*" };foreach (string s in test){    Match m = Regex.Match(s, @"attributeName=([""']?)(?<name>[^""'\s]*)\1", RegexOptions.IgnoreCase);    if (m.Success)        richTextBox1.Text += m.Groups["name"].Value + "\n";} 

热点排行