Home > Enterprise >  How to get a substring with Regex in Python
How to get a substring with Regex in Python

Time:01-28

I am trying to formnulate a regex to get the ids from the below two strings examples:

  1. /drugs/2/drug-19904-5106/magnesium-oxide-tablet/details
  2. /drugs/2/drug-19906/magnesium-moxide-tablet/details

In the first case, I should get 19904-5106 and in the second case 19906.

So far I tried several, the closes I could get is [drugs/2/drug]-.*\d but would return g-19904-5106 and g-19907. Please any help to get ride of the "g-"? Thank you in advance.

CodePudding user response:

When writing a regex expression, consider the patterns you see so that you can align it correctly. For example, if you know that your desired IDs always appear in something resembling ABCD-1234-5678 where 1234-5678 is the ID you want, then you can use that. If you also know that your IDs are always digits, then you can refine the search even more

For your example, using a regex string like

. ?-(\d (?:-\d )*)

should do the trick. In a python script that would look something like the following:

match = re.search(r'. ?-(\d (?:-\d )*)', my_string)
if match:
    my_id = match.group(1)

The pattern may vary depending on the depth and complexity of your examples, but that works for both of the ones you provided

CodePudding user response:

This is the closest I could find: \d |.\d -.\d

  • Related