older
V2EX  ›  问与答

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

  •  
  •   older · Jan 13, 2014 · 3988 views
    This topic created in 4678 days ago, the information mentioned may be changed or developed.
    <a href="ss.html">xxxx</a>

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

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

    你可以在 http://tool.chinaz.com/regex/ 测试正则表达式。
    shiny
    shiny
    PRO
    Jan 13, 2014❤️1
    3
    为什么一定要正则呢,不能用 dom 解析吗?
    older
    older
    OP
    Jan 13, 2014
    4
    <a href="这里不确定的">xxxx</a>
    @34D
    @xinhugo
    xinhugo
    xinhugo
    Jan 13, 2014❤️1
    5
    @older 如果你还有其他的匹配要求,请先给「感谢」,这是基本礼仪。
    strak47
    strak47
    Jan 13, 2014
    6
    <a href=".+\.html">\w*<\/a>
    123123
    123123
    Jan 13, 2014
    7
    看见一楼回复乐了,简单暴力
    xinhugo
    xinhugo
    Jan 13, 2014❤️1
    8
    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
    34D
    Jan 14, 2014
    9
    @older 不确定去掉就好了。"<a href=\"(?:.*?)\">(.*?)</a>"
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4869 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 09:23 · PVG 17:23 · LAX 01:23 · JFK 04:23
    ♥ Do have faith in what you're doing.