1

I am parsing a number from a URL string. The URL looks like:

https://www.myapi.com/player/?url=https%3A//myapi.com/users/11468859&color=788b78&auto_play=false&show_artwork=false

I would like to match the number between 'users/' and '&'. In this case '11468859'. So I using a positive lookahead and lookbehind to accomplish this.

This is what I have so far:

(?<=users/)([0-9]*?)(?=\&)

This doesn't match anything. My lookbehind is wrong. So if I omit the lookbehind I can match on users/11468859

([0-9]*?)(?=\&) matches >> 'users/11468859'

How do I correctly create a positive lookbehind to match on users/?

Thanks!

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
Nick
  • 19,198
  • 51
  • 185
  • 312

5 Answers5

3

Putting aside your lookbehind question for a moment, this regex works:

users/([0-9]+)

Regular expression visualization

Debuggex Demo

The id is in capture group one.

In debuggex your lookbehind works fine but not in JavaScript:

(?<=users/)([0-9]*?)(?=\&)

Regular expression visualization

Debuggex Demo

(You could also get away with just

(?<=users/)([0-9]*)

Regular expression visualization

Debuggex Demo

since [0-9]* is greedy.)

However, as you're using JavaScript, I recommend the regex at the top of my answer.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • Wow...Debuggex...sweet. Thanks for sharing! The only problem with the first regex is that it will match `user/`, and I assume he's using lookbehind/ahead because he doesn't want to match it. Excellent answer, though. – muppetjones Apr 02 '14 at 16:16
  • @muppetjones, my stackoverflow Muppet buddy: How will it match `user/`? It's explicitly `users/`. – aliteralmind Apr 02 '14 at 16:18
  • Yeah. That Debuggex is pretty rad. Thanks! – Nick Apr 02 '14 at 16:21
  • The lookbehind matches, but doesn't return those matched characters as a part of the actual match, which could be relevant. Without the lookbehind, `users/` actually matches as a part of the regex. – muppetjones Apr 02 '14 at 16:59
  • I see. Without the possibility of lookbehinds in JavaScript, I don't see another option. The ID is separately captured, so it works. – aliteralmind Apr 02 '14 at 17:02
1

If you're certain that the desired segment will be a series of integers immediately after user/, you don't need the look ahead. Also, I would recommend escaping any sort of slash: \/

(?<=users\/)([0-9]*?)

Also, you don't need to tell the regex not to be greedy unless you know it will run into other numbers, and I would consider telling the regex that there must be numbers so it won't match if they are missing:

thus

([0-9]*?)

becomes

(\d+)
muppetjones
  • 296
  • 3
  • 14
0

There are a couple of approaches avaiable in most languages. To match a number use the positive look ahead fromat (?<=STUFF). To match numbers try \d+ or [0-9]+. Each of the following lines work. The second includes a positive look ahead for including letters in an id but will fail if the ampersand is moved.

(?<=users.)\d+
(?<=users.).*?(?=&)
(?<=users.)[0-9]+

For more information: http://myregextester.com/index.php#highlighttab

Andrew Scott Evans
  • 1,003
  • 12
  • 26
0

How do I correctly create a positive lookbehind to match on users/?

You don't, because JavaScript does not support lookbehinds:

From javascript regex - look behind alternative?:

Javascript doesn't have regex lookbehind.

http://regexadvice.com/forums/thread/58678.aspx:

The JavaScript regex engine does not support look-behinds

As an alternative, you can capture the number like this:

users\/(.*?)\&

And just access the first capturing group. Explanation and demonstration: http://regex101.com/r/aZ3bL0

Community
  • 1
  • 1
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
0

try

string = "https://www.myapi.com/player/?url=https%3A//myapi.com/users/11468859&color=788b78&auto_play=false&show_artwork=false"
regex = /users.([\d]*)/;
arr = regex.exec(a);
result = arr[1];
jeshu911
  • 94
  • 3