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


I have list of webpage URLs, I just need to strip everything except specific value and ID from it using regex

>假设我有遵循以下结构的URL列表。我需要逐一删除,所以剩下的就是 abcustomerid=12345。如何使用带有记事本++的正则表达式来执行此操作?

以下是每行中不同品种的示例。我只需要从每行中删除所有内容,但保留 abcustomerid=12345 或 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

每行在 abcustomerid 周围可能有任何不同的东西,但我只需要删除所有内容并保留 abcustomerid 和值。

这个正则表达式应该可以做到。

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

用法:

<?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]);

?:告诉正则表达式不要捕获该组。我们不想捕获这些数据,因为它无关紧要。()捕获我们感兴趣的数据。'd+是一个或多个数字(+是其中的一个或多个部分)。如果可以是任何值,将其更改为.+?这将匹配任何内容,但您将需要一个锚点来定位它应该停止的位置。我会使用 (?:&|$) ,它告诉它捕获直到下一个&或字符串的末尾,如果它是多行的,则需要使用 m 修饰符。http://php.net/manual/en/reference.pcre.pattern.modifiers.php

输出:

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

演示:http://sandbox.onlinephpfunctions.com/code/37a4ddea8c50f98a41ac7d45fec98f5f1f58761f

这是正则表达式,它采用abcustomerid值。

[?&](abcustomerid='d+)

但是,您将如何使用记事本++"删除所有内容"?
您可以使用此服务来执行此操作(答案末尾有演示)。

将您的正则表达式和所有数据复制到Test string表单中。成功匹配所有内容后Match information查看页面中间右侧的窗口。单击Export matches...按钮并选择plain text

你会得到这样的东西:

abcustomerid=53122
abcustomerid=241
abcustomerid=12525

这是工作演示。