PHP RegEx with or without slashes

My goal:

To capture the last part of a URL, whether or not a trailing slash exists, if the trailing slash is not part of a string at a URL similar to the following:

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

My problem:

Each way I'm trying to do only allows a trailing slash, not a URL without a trailing slash, or does a trailing slash in the string that I want.

What have i tried?

1. I tried to add a slash:

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

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

2. I tried to add a question mark:

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

This leads to a slash, if it exists, inside my line.

My question is / tl; dr:

How can I create RegEx so as not to require a slash, but keep the slash from the previous line?

+4
source share
3 answers

Your .* Is greedy by default, so if it can "eat" a slash in the capture group, this will happen.

To make this non-greedy, you need to .*? instead of .* in your capture group. So your regex will be:

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

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

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

Or, alternatively, you can use the inanimate quantifier in your group, you will need to specify a trailing slash or the end of the line (or some other terminator) so that the group can capture your identifier:

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

Something you could try might be:

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

Demo on regex101

EDIT: Yes, you should have mentioned that you also need to check the site, since you put foo.com in your first line of the example ... (and then edited your question ...).

You can use this instead to check the site:

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

Source: https://habr.com/ru/post/1498257/


All Articles