I'm trying to write a regex that will extract the numbers after directory/
in the following URL:
http://www.website.com/directory/9892639512/alphanum3r1c/some-more-text-here/0892735235
If I know the number of digits, then this regex I wrote works:
directory\/([0-9]{7})\/
However, if I remove the number of digits to match {7}
, then the regex stops working.
Live demo: http://regex101.com/r/wX3eI2
I've been trying different, things, but can't seem to get the regex to work without explicitly setting the number of characters to match.
How can I get this working?
\d for single or multiple digit numbers It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range. [1-9][0-9] will match double digit number from 10 to 99.
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.
match(/(\d{5})/g);
Add the $ anchor. /^SW\d{4}$/ . It's because of the \w+ where \w+ match one or more alphanumeric characters. \w+ matches digits as well.
Change regex to:
directory\/([0-9]+)\/
The {7}
means, 7 characters (in this case only numbers). The +
means one or more characters (in this case numbers).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With