*****高分求正则******

阿飞7587 2012-12-26 12:14:03
求一正则,要求实现:
如果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
...全文
154 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
string str = " www.bj.com/p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3"; var ary=Regex.Matches(str, @"(?i)(?<=/([a-z]+\d+)*)[a-z]+\d+|(?<=\?([^&$]+&)*)[^&$]+")……
高手!昨天写了半天终于找到了关于MatchCollection的Lambda的写法
H_Gragon 2012-12-26
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
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……
同意!
q2104574 2012-12-26
  • 打赏
  • 举报
回复
(\w\d+)|(\w*\d*=\w*\d*)
  • 打赏
  • 举报
回复
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();
  • 打赏
  • 举报
回复
上面正则分组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=
  • 打赏
  • 举报
回复
 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();
        }
阿飞7587 2012-12-26
  • 打赏
  • 举报
回复
上面的规则:<rewrite url="^/test/([\w\W]*)*?/?$" to="~/index.aspx?placeholder=$1" /> 对于字符串url:/test/p3m3h4t4o2d1c8z4n1u4/?pmin=0&pmax=99999&amin=0&amax=99999&near=&tag=3_2&key= 只能匹配到: p3m3h4t4o2d1c8z4n1u4/?pmin=0 ,&后面的都找不到了, 正则应该如何改进呢?
  • 打赏
  • 举报
回复
\S+
阿飞7587 2012-12-26
  • 打赏
  • 举报
回复
大家帮助看下
阿飞7587 2012-12-26
  • 打赏
  • 举报
回复
如果我要url重写,将"http://www.bj.com/p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3"; 中的p1a33c33d2f4g9h11m2/?type=bb&fac=2&d=3";给提取出来,如何写呢? 比如: <rewrite url="^/sale/(此处正则如何写)$" to="~/index.aspx?placeholder=$1" />

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧