简体   繁体   中英

PHP RegEx with or without trailing slashes

My goal:

To capture the last part of a URL whether there is or isn't a trailing slash, without the trailing slash being a part of the string on a URL similar to the one following:

http://foo.com/p/dPWjiVtX-C/
                 ^^^^^^^^^^
               The string I want

My issue:

Every way I try only allows for a trailing slash and not for a url without a trailing slash or makes the trailing slash be contained in the string I want.

What have I tried?

1. I have tried to add a slash to the end:

  $regex = "/.*?foo\.com\/p\/(.*)\//";
  if ($c=preg_match_all ($regex, $url, $matches))
  {
    $id=$matches[1][0];
    print "ID: $id \n";
  }

This results in error when I don't have a trailing slash.

2. I have tried to add a question mark:

  $regex = "/.*?foo\.com\/p\/(.*)[\/]?/";

This results in the slash, if exists, being inside my string.

My question/tl;dr:

How can I build a RegEx to not require a slash, yet keep the slash out of my preceding string?

Your .* is greedy by default, so if it can "eat" the slash in the capturing group, it will.

To make it not greedy, you need .*? in the place of the .* in your capturing group. So, your regex will be:

$regex = "/^.*?instagram\.com\/p\/(.*?)[\/]?$/";

You can use this to capture all characters except the trailing slash in your group:

$regex = "/.*?instagram\.com\/p\/([^\/]*)/"

Or alternatively, you can use a non-greedy quantifier in your group, you'll have to specify a trailing slash or the end of the string (or some other terminator) in order for the group to capture your id:

$regex = "/.*?instagram\.com\/p\/(.*?)(?:\/|$)/"

Something you might try perhaps:

([^\/]+)\/?$

Demo on regex101

EDIT: Huh, you should have mentioned you need to check the site as well, since you put foo.com in your first example string... (and re-edited your question after that...).

You can use this instead to check the site:

^.*foo\.com.*?([^\/]+)\/?$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM