regex to filter out numbers in seo url
I have some urls like these below
http://www.bla-bla.com/hello-world/blah/1345346-asfasdf.html
http://www.bla-bla.com/hello-world/454536556-asdf-rtrthr-dssdfg.html
http://www.bla-bla.com/hello-world/bla/how/what/26609768-nmbbasdf.html
IF the url has a slash followed by numbers, I need to return the just numbers so the result must be
1345346
454536556
26609768开发者_如何学编程
How can I get everything but the numbers from urls
If those are the only numbers in your URL, you can simply use /\d+/
, which stands for "Any digit one or more times".
If you need to specifically group out the numbers in the final part of the string, you can use something more like this: /\/(\d+).*\.html$/
, which stands for "A group of digits, following a literal forward slash '/', followed by any characters and .html at the end of a string", and capture group 1 would contain it.
As per request from comment: to get the numbers preceded by a forward slash /
and ending with a hyphen -
, just use this: /(?<=\/)\d+(?=\-)/
, which can be broken down as:
(?<=\/) # Look before the group for a forward slash, but don't add it to the capture group.
\d+ # Match one or more digits (0-9)
(?=\-) # Look after the group for a hyphen, but don't add it to the capture group.
Try using this as your regular expression: /\/([0-9]+)/
精彩评论