Get text between last forward slash and then before first hyphen
Solution 1
Use the below regex and get the number from group index 1.
^.*\/([^-]*)-.*$
Code:
>>> import re
>>> s = "http://www.example.com/0/100013573/1585710-key-description-goes-here"
>>> m = re.search(r'^.*\/([^-]*)-.*$', s, re.M)
>>> m
<_sre.SRE_Match object at 0x7f8a51f07558>
>>> m.group(1)
'1585710'
>>> m = re.search(r'.*\/([^-]*)-.*', s)
>>> m.group(1)
'1585710'
>>> m = re.search(r'.*\/([^-]*)', s)
>>> m.group(1)
'1585710'
Explanation:
-
.*\/
Matches all the characters upto the last/
symbol. -
([^-]*)
Captures any character but not of-
zero or more times. -
-.*
Matches all the remaining characters. -
group(1)
contains the characters which are captured by the first capturing group. Printing the group(1) will give the desired result.
Solution 2
Well, if you need to find any strings between a /
and a -
, you could simply do:
/.*-
Since .
is any char, and *
is any amount. However, this poses a problem, because you could get the whole /www.example.com/0/100013573/1585710-key-description-goes
, which is between /
and a -
. So, what you need to do is to search for anything that is not a /
and -
:
/[^/-]*-
^
means no, and anything between []
is, roughly, an OR list.
Hope that helps.
EDIT: No, it doesn't help, as user rici mentioned, when you have a -
in your url name (as in www.lala-lele.com
).
To make sure is the last /
you got, you can match the rest of your string, making sure it doesn't have any /
in it until the end ($
), as in:
/[^/-]*-[^/]*$
And, to get just the string inside it, you can:
/\([^/-]*\)-[^/]*$
Since \(
and \)
specify what you want as the output of your regex.
Solution 3
You can use matching groups in order to extract the number with the regex \/(\d+)-
:
import re
s = 'http://www.example.com/0/100013573/1585710-key-description-goes-here'
m = re.search(r'\/(\d+)-', s)
print m.group(1) # 1585710
Check out the Fiddler
Andy
Updated on August 26, 2022Comments
-
Andy over 1 year
I need to parse a URL, and get 1585710 from :
http://www.example.com/0/100013573/1585710-key-description-goes-here
So that means it's between the last / and before the first -
I have very little experience with regex, it's a really hard concept for me to understand.
Any help or assistance would be much appreciated
Edit: Using Python.
-
rici over 9 yearsOP wants the string between the last slash and the next dash. Your regex will fail, for example, on
/www.best-example.com/0/100013573/1585710-key-description-goes