drbouzlxb92333332 2015-06-19 03:27
浏览 49

我有网页网址列表,我只需要使用正则表达式从中删除除特定值和ID之外的所有内容

Suppose I have list of URLs that follow structure below. I need to strip each one out so all thats left is the abcustomerid=12345. How can I do this using regex with notepad ++?

Here's an example of the different variety in each line. I just need to remove everything from each line, but leave the abcustomerid=12345 or whatever value that follows abcustomerid.

/the/stucture/blah.php?timeout=300&abcustomerid=53122&customer=zxyi
/some/other/struct/pagehere.php?today=Thursday&abcustomerid=241&count=54
/blah/blah/tendid.php?abcustomerid=12525

Each line could have anything different around the abcustomerid, but i just need to remove everything and keep the abcustomerid and the value.

  • 写回答

2条回答 默认 最新

  • duanmeng9336 2015-06-19 03:38
    关注

    This regex should do it.

    (?:&|\?)abcustomerid=(\d+)
    

    Usage:

    <?php
    $string= '/the/stucture/blah.php?timeout=300&abcustomerid=53122&customer=zxyi
    /some/other/struct/pagehere.php?today=Thursday&abcustomerid=241&count=54
    /blah/blah/tendid.php?abcustomerid=12525';
    preg_match_all('~(?:&|\?)abcustomerid=(\d+)~', $string, $output);
    print_r($output[1]);
    

    The ?: tells the regex not to capture that group. We don't want to capture that data because it is irrelevant. The () capture the data we are interested in. The \d+ is one or more numbers (the + is the one or more part of it). If it can be any value change that to .+? which will match anything but then you will need an anchor for where it should stop. I'd use (?:&|$), which tells it to capture until the next & or the end of the string if it is multilined you'll need to use the m modifier. http://php.net/manual/en/reference.pcre.pattern.modifiers.php

    Output:

    Array
    (
        [0] => 53122
        [1] => 241
        [2] => 12525
    )
    

    Demo: http://sandbox.onlinephpfunctions.com/code/37a4ddea8c50f98a41ac7d45fec98f5f1f58761f

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度