日期:2014-05-19  浏览次数:20993 次

急求一个正则表达式
字符串{aa$TTT$aa}{aa$KK$aa}
我要获取TTT,KK字符串需要怎么写啊?谢谢!

------解决方案--------------------
Regex re = new Regex(@ "{aa\$(? <name> [^$]*)\$aa} ", RegexOptions.None);
MatchCollection mc = re.Matches( "text ");
foreach (Match ma in mc)
{
Console.WriteLine(ma.group[ "name "].value);
}

------解决方案--------------------
- -
你有固定的格式么?
不然这样每种都需要写一遍

针对: <aa> TTT </aa> <aa> KK </aa>

Regex re = new Regex(@ " <aa> (? <name> .*?) </aa> ", RegexOptions.None);
MatchCollection mc = re.Matches( "text ");
foreach (Match ma in mc)
{
Console.WriteLine(ma.group[ "name "].value);
}
------解决方案--------------------
string reg = @ "\$(? <str> [^\$]*)\$ ";
string str = " <aa> $TTT$ <aa> {aa$KK$aa} ";
MatchCollection matchCollection = Regex.Matches (str , reg , RegexOptions.Multiline);
foreach (Match ma in matchCollection)
{
Response.Write (ma.Groups[ "str "] + " <br /> ");
}