11

I'm trying to do a positive lookahead to match an object ID in a given URL, regardless of where that object ID is in the URL. The idea being to match until either a '/' or the end of the string. Here are some sample strings (bold being the ID I want to match):

  • /objects/obj_asd-1234-special
  • /objects/obj_xyz-15434/members
  • /objects/obj_aasdfaeastd-15d44/other/asdf

Using this: objects/obj_(.+?)(?=/) matches the latter two as they both have a trailing slash. I read that the lookahead supports regex as the matching character, so I tried this objects/obj_(.+?)(?=(/|$)) to no avail. Any thoughts?

tchrist
  • 78,834
  • 30
  • 123
  • 180
devights
  • 263
  • 1
  • 4
  • 11

3 Answers3

9

This regex matches either EOF or a certain character (i.e. /).

(?<=\/objects\/obj_)(.+?)(?=\/|$)

Here is a demo.

sluijs
  • 4,146
  • 4
  • 30
  • 36
6

Try this:

/objects/(.*?)(/|$)

It simply does a non-greedy match between /objects/ and either a slash or eof

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

You don't need to use a positive lookahead:

/objects/([^/]+).*

And then, the first group will hold your id value.

Here's an example in Python:

>>> import re
>>> p = re.compile('/objects/([^/]+).*')

>>> p.match("/objects/obj_asd-1234-special").group(1)
'obj_asd-1234-special'
>>> p.match("/objects/obj_xyz-15434/members").group(1)
'obj_xyz-15434'
>>> p.match("/objects/obj_aasdfaeastd-15d44/other/asdf").group(1)
'obj_aasdfaeastd-15d44'
João Silva
  • 89,303
  • 29
  • 152
  • 158