正则表达式从URL获取ID

时间:2014-05-05 19:13:06

标签: php regex

如何从这些给定的url字符串中提取ID123

my-domain/product/name-product-ID123.html
my-domain/product/name-product-ID123.html/
my-domain/product/name-product-ID123.html?bla=123&some=456

如果不是ID,则是一个长度等于2 (AB, EF, GH, ...)

的随机字符串

有人可以帮帮我吗?

5 个答案:

答案 0 :(得分:2)

这可能不是正则表达式的工作,而是适用于您选择的语言的现有工具。正则表达不是一个魔术棒,您可以在遇到涉及字符串的每个问题上挥手。您可能希望使用已编写,测试和调试的现有代码。

在PHP中,使用parse_url函数。

Perl:URI module

Ruby:URI module

.NET:'Uri' class

答案 1 :(得分:0)

这就是我提出的:

(?!-)(ID[0-9]*)(?=\.)

测试: http://regex101.com/r/rP0vI2

如果不是" ID",那么它将是:

(?!-)([A-Z]{2}[0-9]*)(?=\.)

经过测试:http://regex101.com/r/dW8qK0

答案 2 :(得分:0)

$zeichenkette = "my-domain/product/name-product-ID123.html"; $suchmuster = '/ID[0-9]{3}/'; preg_match($suchmuster, $zeichenkette, $treffer, PREG_OFFSET_CAPTURE, 3); print_r($treffer);

应该打印ID123。

答案 3 :(得分:0)

试试这个:

(?<=product-)ID[0-9]+(?=\.html)
  • (?<=product-)肯定的Lookbehind - 断言ID前面有字符串product-

  • ID与字符ID字面匹配

  • [0-9]+匹配数字序列

  • (?=\.html)肯定前瞻 - 断言ID后跟.html

答案 4 :(得分:0)

简短而有效:

<?php
$links = <<< LOB
my-domain/product/name-product-ID123.html
my-domain/product/name-product-ID123.html/
my-domain/product/name-product-ID123.html?bla=123&some=456
LOB;

preg_match_all('/-(ID\d+)\./',$links ,$ids, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($ids[1]); $i++) {
    echo $ids[1][$i]."\n";
}
/*
ID123
ID123
ID123
*/
?>

现场演示:
http://ideone.com/OqhL6b

<强> 说明:

Match the character “-” literally «-»
Match the regular expression below and capture its match into backreference number 1 «(ID\d+)»
   Match the characters “ID” literally «ID»
   Match a single digit 0..9 «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “.” literally «\.»
相关问题