这两天又花了点时间看了一下Regular Expression的语法,顺便写了一个用于 匹配出all continuous white-spaces outside the C-style multiline comment blocks的Regex。这个是我个人到目前为止写的一个算是最复杂的正则表达式了,贴出来与大家交流:

(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*(?:/\*))|(?<=(?:\*/)(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)|(?<=\A(?:[^/\*]|(?<!/)\*|/(?!\*))*)\s+(?=(?:[^\*/]|(?<!\*)/|\*(?!/))*\z)

当然小弟我对Regular Expression接触不多,这个Regex应该还有问题,肯定有些特殊的情况没有考虑到,请大家指正:)

另外还有个问题想请教大家。我在看MSDN中Regular Expression Language Elements的帮助的时候,有一个Grouping Construct没有看懂:

(?>   ) Nonbacktracking subexpression (also known as a "greedy" subexpression). The subexpression is fully matched once, and then does not participate piecemeal in backtracking. (That is, the subexpression matches only strings that would be matched by the subexpression alone.)

如果有谁知道它的含义,请告知。我想这个不仅是我一个人想知道:)当然另外如果有人在学习正则表达式的过程如果遇到过什么问题,也欢迎提出来交流一下:)

下面是MSDN中的Regular Expression Language Elements的Reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconregularexpressionslanguageelements.asp

最后,给大家推荐两个关于Regular Expression的很不错的学习网站:)
http://www.regexlib.com/
http://www.regular-expressions.info/


[05/04/20 Update]:

哈哈, 今天又写了一个更长的Regular Expression,全长527,是用于查找出所有的XHTML/HTML的标记外面的所有空格,并将之转换为&nbsp;的。

(?:(?:\<(?:Style)(?:\s+(?:[\w-]+)(?:=(?:[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)(?:[\s\S]*?)(?:\</(?:Style)\>))|(?:(?:\<(?:script)(?:\s+(?:[\w-]+)(?:=(?:[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)(?:[\s\S]*?)(?:\</(?:script)\>))|(?:\<!(?:[\w-]+)(?:\s+(?:[\w-]+|\"[\s\S]*?\"|\'[\s\S]*?\'))*\s*\>)|(?:\<!--[\s\S]*?--\>)|(?:\<(?:[\w-]+)(?:\s+(?:[\w-]+)(?:=(?:[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)|(?:\</(?:[\w-]+)\>)|(?:\<!\[CDATA\[(?:[\s\S]*?)\]\]\>)|(?:(?:(?<blank>[ ]+)|[^ \<\>])+)

这个正则表达式虽然很长,但可不是我用手code出来的哦,是我写的程序产生的,代码如下:)


public static string ReplaceSpace(string content)
{
string tag = @"(?:[\w-:]+)";
string attribute = @"(?:[\w-:]+)(?:=(?:[^\s\>\<]*|\""[\s\S]*?\""|\'[\s\S]*?\'))?";
string name = @"(?:[\w-:]+)";
string argument = @"(?:[\w-:]+|\""[\s\S]*?\""|\'[\s\S]*?\')";

string beginningTag = @"(?:\<" + tag + @"(?:\s+" +attribute + @")*\s*(?:/)?\>)";
string endingTag = @"(?:\</" + tag + @"\>)";
string xmlComment = @"(?:\<!--[\s\S]*?--\>)";
string xmlDirective = @"(?:\<!" +name + @"(?:\s+" +argument + @")*\s*\>)";
string xmlCData = @"(?:\<!\[CDATA\[(?:[\s\S]*?)\]\]\>)";
string styleBlock = @"(?:(?:\<(?:Style)(?:\s+" +attribute + @")*\s*(?:/)?\>)(?:[\s\S]*?)(?:\</(?:Style)\>))";
string scriptBlock = @"(?:(?:\<(?:script)(?:\s+" +attribute + @")*\s*(?:/)?\>)(?:[\s\S]*?)(?:\</(?:script)\>))";
string xmlLiteral = @"(?:(?:(?<blank>[ ]+)|[^ \<\>])+)";

string pattern = styleBlock + "|" + scriptBlock + "|" + xmlDirective + "|" + xmlComment + "|" + beginningTag + "|" + endingTag + "|" + xmlCData + "|" + xmlLiteral;

Regex r
= new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);

MatchCollection mc
= r.Matches(content);

StringBuilder sb
= new StringBuilder(content.Length + 1024);
foreach (Match m in mc)
{
if (m.Groups["blank"].Captures.Count > 0)
{
sb.Append(m.Value.Replace(
" ", "&nbsp;"));
}

else
{
sb.Append(m.Value);
}

}

return sb.ToString();
}


希望这个能够解决今天dudu提的一个cnblogs的bug:)即使不能解决问题也算是对System.Text.RegularExpression.Regex的一个练笔了:)

最后,再给个更长的(全长765)正则表达式,这个算是我写的和见过的最长的正则表达式了。
虽然长,但很有用,可以解析出整个XHTML/HTML页面的元素和结构来:)

(?#Copyright 2005, by Laser Lu.)(?<Style_Block>(?<begin>\<(?<tag>style)(?:\s+(?<attribute>[\w-:]+)(?:=(?<value>[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)(?<body>[\s\S]*?)(?<end>\</\k<tag>\>))|(?<Script_Block>(?<begin>\<(?<tag>script)(?:\s+(?<attribute>[\w-:]+)(?:=(?<value>[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)(?<body>[\s\S]*?)(?<end>\</\k<tag>\>))|(?<XML_Directive>\<!(?<name>[\w-:]+)(?:\s+(?<argument>[\w-:]+|\"[\s\S]*?\"|\'[\s\S]*?\'))*\s*\>)|(?<XML_Comment>\<!--[\s\S]*?--\>)|(?<Beginning_Tag>\<(?<tag>[\w-:]+)(?:\s+(?<attribute>[\w-:]+)(?:=(?<value>[^\s\>\<]*|\"[\s\S]*?\"|\'[\s\S]*?\'))?)*\s*(?:/)?\>)|(?<Ending_Tag>\</(?<tag>[\w-:]+)\>)|(?<XML_CDATA>\<!\[CDATA\[(?<data>[\s\S]*?)\]\]\>)|(?<XML_Literal>(?:(?<blank>[ ]+)|[^ \<\>])+)

posted on 2005-04-20 00:42  Laser.NET  阅读(11100)  评论(22编辑  收藏  举报
无觅相关文章插件,快速提升流量