日期:2014-05-18  浏览次数:20734 次

正规则问题

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";
}