goldisfine
goldisfine

Reputation: 4850

Characters following a slash and ending before a hyphen

I've been playing around with regex for a bit and regex visualizations, but have had no luck in generating something that will match a section of a url that is text of variable length preceeded by a forward slash and terminated by a hyphen. What expression would do this?

www.lamp.com/;alskfjdlkfja;sdlkfjasldfj-209

but not

www.lamp.com/a;slkfja;sdlkfjas;dflkj

because that doesn't contain a hyphen

Upvotes: 0

Views: 85

Answers (2)

Fred Mitchell
Fred Mitchell

Reputation: 2161

Might you first want to start with urlparse, http://docs.python.org/2/library/urlparse.html, to get to the section of a url, but only if it really matched.

Upvotes: 0

arshajii
arshajii

Reputation: 129497

You can try something like this:

/[^-]+-

where:

  • / is a literal /
  • [^-]+ is one or more non-hyphens
  • - is a literal -

Using your examples:

>>> import re
>>> url1 = 'www.lamp.com/;alskfjdlkfja;sdlkfjasldfj-209'
>>> url2 = 'www.lamp.com/a;slkfja;sdlkfjas;dflkj'
>>> 
>>> re.search(r'/[^-]+-', url1) is not None
True
>>> re.search(r'/[^-]+-', url2) is not None
False

Upvotes: 4

Related Questions