读书人

*****高分求正则******解决

发布时间: 2013-01-11 11:57:35 作者: rapoo

*****高分求正则******
求一正则,要求实现:
如果url是:www.bj.com/p1a33c33d2f4g9h11m2/
通过正则能提取出: p1、a33、c33、d2、f4、g9、h11、m2

如果url是:www.bj.com/p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3
通过正则能提取也:p1、a33、c33、d2、f4、g9、h11、m2、type=bb、fac=2、d=3

需要注意:p1a33c33d2f4g9h11m2,字母占位符顺序不是固定的,也有可能
a33c33p1d2f4h11g9m2
[解决办法]

 static void Main(string[] args)
{
Func<string, string[]> getvalues = (text) =>
{
string[] txts = text.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
List<string> list = new List<string>();
MatchCollection mc = Regex.Matches(txts[1], "[a-z][0-9]+");
string[] res = new string[mc.Count];
for (int i = 0; i < mc.Count; i++)
{
list.Add(mc[i].Groups[0].Value);
}
if (txts.Length > 2)
{
if (txts[2].Contains('&'))
{
txts[2] = txts[2].Contains('?') ? txts[2].Replace("?", "") : txts[2];
txts[2].Split('&').ToList().ForEach(x => list.Add(x));
}
else
{
list.Add(txts[2]);


}
}
return list.ToArray();

};
string tex = "www.bj.com/p1a33c33d2f4g9h11m2/";
string[] p = getvalues(tex);
p.ToList().ForEach(x => Console.WriteLine(x));
Console.WriteLine("=============分割线=============");
string tex2 = "www.bj.com/p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3";
string[] p2 = getvalues(tex2);
p2.ToList().ForEach(x=>Console.WriteLine(x));
Console.Read();
}


[解决办法]
string str = " www.bj.com/p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3";
var ary=Regex.Matches(str, @"(?i)(?<=/([a-z]+\d+)*)[a-z]+\d+
[解决办法]
(?<=\?([^&$]+&)*)[^&$]+").OfType<Match>().Select(t => t.Value).ToArray();

[解决办法]
(\w\d+)
[解决办法]
(\w*\d*=\w*\d*)

[解决办法]
\S+
*****高分求正则******解决方法
[解决办法]
上面正则分组1可以正常匹配,虽然写的有点罗嗦
string pattern = @"^/test/([\w\W]*)*?/?$";
string tempStr = "/test/p3m3h4t4o2d1c8z4n1u4/?pmin=0&pmax=99999&amin=0&amax=99999&near=&tag=3_2&key=";

string result = Regex.Match(tempStr,pattern).Groups[1].Value;
//p3m3h4t4o2d1c8z4n1u4/?pmin=0&pmax=99999&amin=0&amax=99999&near=&tag=3_2&key=

读书人网 >asp.net

热点推荐