&brvbarzyciis &brvbar 求正则,提取/view_001_002_003.html中的001,002,003
admin 发表于 2010-04-24 | 来源:互联网 | 阅读:

提取/view_001_002_003.html中的001,002,003
1:要以"/view"开头
2:以".html"结束
取中中间的
“_”开头接其他不为换行的字符

如上
/view_001_002_003.html
结果要为
Group[0].Value=http://topic.csdn.net/u/20100422/14/"/view_001_002_003.html"
Group[1].Value=http://topic.csdn.net/u/20100422/14/"001"
Group[2].Value=http://topic.csdn.net/u/20100422/14/"002"
Group[3].Value=http://topic.csdn.net/u/20100422/14/"003"

谢谢

当个或固定个我的会写
如view_[.]*\.html
但不同的是我中间的"_XXXX"为示知个数
谢谢

已经有16 个评论
  1. mikeyao210 说:

    请使用正则表达式进行匹配

  2. doing7 说:

    进行两次进则吗先取出_001_002_003再进行一次正则吗?没有办法一次取出吗?

  3. wtaxum 说:

    一次性使用正则不需要取出来

  4. xiaobao520 说:

    等等 谁个方法给你 你看看 稍微等下

  5. hahamy 说:

    upupup

  6. yyyy6 说:

    先取出_001_002_003 在截取下吧

  7. 风子 说:

    @"(?i)/view(?:_([^_.])*)*\.html"不是用 Groups而是用 Captures

  8. 风子 说:

    C# code
    正则 String pattern = @"/view_(/d{3})_(/d3)_(/d3).html$";
    C# code

    //接收要判断的字符串
    String str = "/view_001_002_003.html";
    //对字符串判断的正则表达示 –为数字定义变量 如cid 方式:?<cid> @是绝对字符 \n等字符会被判定为 "\n" ^表达示$ 绝对匹配

    String pattern = @"/view_(?<cid>/d{3})_(?<cid2>/d3)_(?<cid3>/d3).html$";
    //实例化一个Regex类
    Regex re = new Regex(pattern);
    //调用Match方法
    Match mc = re.Match(str);
    //进行判断 并接收值
    if (mc.Success)
    {
    String cid = mc.Groups["cid"].Value;
    String page = mc.Groups["page"].Value;

    Label1.Text = "abc_" + cid + "_" + page + ".html";
    }

  9. lchz 说:

    后面的没改 这个是我资料里的 改了一段发出来 你看看 学习下

  10. weiwei 说:

    @"(?i)/view(?:_([^_.]*))*\.html"

  11. 风子 说:

    C# code

    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(?i)/view(?:_([^_.])*)*\.html");
    var m = reg.Match("/view_001_002_003.html");
    if (m.Success)
    {
    var c = m.Captures;
    }

    上面的怎么写啊。。一次就取出的

  12. 风子 说:

    C# code
    /view_(\d)+_(\d)+_(\d)+\.html

  13. jc2008_828 说:

    /view_(\d)+_(\d)+_(\d)+\.html———晕。我都说了,里面的参数是不固定数量的

  14. 405290688 说:

    C# code
    using System;
    using System.Text.RegularExpressions;

    class Program
    {
    static void Main()
    {
    string s = "/view_001_002_003.html";
    Match m = Regex.Match(s, @"(?i)/view(?:_([^_.]+))*\.html");
    Console.WriteLine("[{0}]", m.Groups[0]);
    foreach (Capture t in m.Groups[1].Captures)
    {
    Console.WriteLine("[{0}]", t);
    }
    }
    }
    /* 程序输出:
    [/view_001_002_003.html]
    [001]
    [002]
    [003]
    */

  15. yyyy6 说:

    Match.Groups[i].ValueMatch.Groups[i].Captures[j].Value是字符串。

  16. dovemail 说:

    /view_(\d)+_(\d)+_(\d)+\.html———晕。我都说了,里面的参数是不固定数量的C# code
    /view(_(\d)+)+\.html

我要评论

评论功能因故关闭!

请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)


返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 友情链接 | 版权声明