V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
older
V2EX  ›  问与答

正则表达式如何匹配链接内的文本

  •  
  •   older · 2014-01-13 15:54:20 +08:00 · 3084 次点击
    这是一个创建于 3768 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <a href="ss.html">xxxx</a>

    如何找到xxxx
    9 条回复    1970-01-01 08:00:00 +08:00
    34D
        1
    34D  
       2014-01-13 16:12:44 +08:00   ❤️ 2
    ‘<a href="ss.html">(.*?)</a>’
    xinhugo
        2
    xinhugo  
       2014-01-13 16:13:58 +08:00
    因为不知道你要求的「xxxx」是什么内容,暂且理解为字母或数字或下划线或汉字。

    那么,正则表达式则为:<a href="ss.html">\w+</a>

    你可以在 http://tool.chinaz.com/regex/ 测试正则表达式。
    shiny
        3
    shiny  
       2014-01-13 16:14:41 +08:00   ❤️ 1
    为什么一定要正则呢,不能用 dom 解析吗?
    older
        4
    older  
    OP
       2014-01-13 16:15:22 +08:00
    <a href="这里不确定的">xxxx</a>
    @34D
    @xinhugo
    xinhugo
        5
    xinhugo  
       2014-01-13 16:20:07 +08:00   ❤️ 1
    @older 如果你还有其他的匹配要求,请先给「感谢」,这是基本礼仪。
    strak47
        6
    strak47  
       2014-01-13 17:01:10 +08:00
    <a href=".+\.html">\w*<\/a>
    123123
        7
    123123  
       2014-01-13 17:10:50 +08:00
    看见一楼回复乐了,简单暴力
    xinhugo
        8
    xinhugo  
       2014-01-13 17:22:16 +08:00   ❤️ 1
    1.要求:
    1)匹配 <a href="ss.html">xxxx</a> 中的 xxxx。
    2)需要匹配的字符,为4个。其中包括字母或数字或下划线,但不包括中文。

    结果:<a href="ss.html">\w{4}</a>



    2.要求:
    1)匹配 <a href="ss.html">xxxx</a> 中的 ss
    2)需要匹配的字符,为1个或更多。其中包括字母或数字或下划线,但不包括中文。

    结果:<a href="\w+.html">xxxx</a>


    3.要求:
    1)匹配 <a href="ss.html">xxxx</a> 中的 ss
    2)需要匹配的字符,为1个或更多。其中包括字母或数字或下划线或中文。

    结果:<a href=".+.html">xxxx</a>
    34D
        9
    34D  
       2014-01-14 17:19:40 +08:00
    @older 不确定去掉就好了。"<a href=\"(?:.*?)\">(.*?)</a>"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1322 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:15 · PVG 07:15 · LAX 16:15 · JFK 19:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.