Reputation: 135
How can I extract ID123
from these given url strings?
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
And if not ID
, a random string of length equal to 2 (AB, EF, GH, ...)
Can someone please help me?
Upvotes: 0
Views: 1579
Reputation: 98881
Short and effective:
<?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
*/
?>
LIVE DEMO:
http://ideone.com/OqhL6b
Explanation:
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 «\.»
Upvotes: 0
Reputation: 59
This is what I came up with:
(?!-)(ID[0-9]*)(?=\.)
Tested: http://regex101.com/r/rP0vI2
If not "ID", then it would be:
(?!-)([A-Z]{2}[0-9]*)(?=\.)
Tested: http://regex101.com/r/dW8qK0
Upvotes: 0
Reputation: 93636
This might not be a job for regexes, but for existing tools in your language of choice. Regexes are not a magic wand you wave at every problem that happens to involve strings. You probably want to use existing code that has already been written, tested, and debugged.
In PHP, use the parse_url
function.
Perl: URI
module.
Ruby: URI
module.
.NET: 'Uri' class
Upvotes: 2
Reputation: 10618
Try this:
(?<=product-)ID[0-9]+(?=\.html)
(?<=product-)
Positive Lookbehind - Asserts that the ID is preceded by the string product-
ID
matches the characters ID literally
[0-9]+
matches a sequence of digits
(?=\.html)
Positive Lookahead - Asserts that the ID is followed by .html
Upvotes: 0
Reputation: 41
$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);
should print ID123.
Upvotes: 0