Home > OS >  Getting the number in the URL in between slashes
Getting the number in the URL in between slashes

Time:12-26

Not able to get the number in between the two slashes.

For example, the string I have is https://testingjustcheck.com/adm/inventory/inventory/2261695/change/?_changelist_filters=q=&q-l=off.

Here, I wanted to get 2261695. For that, I am using this /\/(\d )/ regex. The output I am getting is /2261695,2261695.

I checked at https://regex101.com/r/XJcoWB/1 also. How to modify the regex to get only 2261695?

CodePudding user response:

You can use this pattern (?<=/)\d (?=/):

  • (?<=/): Occurrence must be preceded by a slash
  • \d : Occurrence must a (multiple) digit(s)
  • (?=/): Occurrence must be followed by a slash

Here is an example on Regex 101.


Note

Depending on the usage, you might need to escape to slash, i.e. (?<=\/)\d (?=\/). (Not needed in Python, required on regex101 though).


Example with Python

import re

re.findall(r"(?<=/)\d (?=/)", "https://testingjustcheck.com/adm/inventory/inventory/2261695/change/?_changelist_filters=q=&q-l=off")
# ['2261695']
  • Related