Home > Mobile >  not getting the number in the URL which is in between the slashes
not getting the number in the URL which is in between the slashes

Time:12-25

Not able to get the number in between the 2 slashes

for e.g. string 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 Regex as this "//(\d )/" But the output I am getting it as /2261695,2261695 I checked in https://regex101.com/r/XJcoWB/1 also. How to modify 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