vking
V2EX  ›  PHP

关于正则表达式的问题.

  •  
  •   vking · Jul 30, 2013 · 3893 views
    This topic created in 4844 days ago, the information mentioned may be changed or developed.
    $url='www.*.com/*/100.html*'
    有什么简单的办法把100提取并且赋给$id
    *里面肯定不会有.html
    100只是个例子,可能是其他数字
    6 replies  •  1970-01-01 08:00:00 +08:00
    123123
    123123
    Jul 30, 2013
    1
    preg_match('~(\d+).html~', $url, $match);

    去看看正则三十分钟
    123123
    123123
    Jul 30, 2013
    2
    更正上面正则中的「.」应该是「\.」,虽然结果没什么差
    vking
    vking
    OP
    Jul 30, 2013
    3
    @123123 谢谢.以前看过发现重新看又得花几个小时.
    BeijingBaby
    BeijingBaby
    PRO
    Jul 30, 2013
    4
    '|/(\d+)\.html|'
    turing
    turing
    Jul 30, 2013❤️1
    5
    lz 搞不清正则不放用语义化的正则包装:https://github.com/jehna/VerbalExpressions
    lazygunner
    lazygunner
    Jul 31, 2013
    6
    import re
    p = re.compile(r'^http://www\..*\.com/.*/(?P<num>\d*)\.html$')
    r = p.search('www.v2ex.com/t/123/html')
    r.group('num')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1309 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:39 · PVG 05:39 · LAX 13:39 · JFK 16:39
    ♥ Do have faith in what you're doing.