preg_match_all但是如果通配符包含斜杠则停止

Assuming I have this regular expression: '#artists/(.*)/#' and I want to match this string: '/artists/alesana/wires-and-the-concept-of-breathing/' how can I make sure that it only matches 'alesana' and not 'alesana/wires-and-the-concept-of-breathing'.

So in other words, how can I let my reg exp cohere to the slashes. Technically I am gonna make another routing rule for artists/(.*)/(.*) but I know I'm gonna run in this problem somewhere else sooner or later.

Make the regular expression non-greedy with the ? character. This will essentially find the shortest match possible:

/artists/(.*?)/

Read more: Lazy quantification in regular expressions

have you try this? artists/[^/].+?/

Another solution than to use the non-greedy search as Hans Engel mentioned would be:

/artists/([^/]*)/

Using a ^ inside a character class [] negates the content. Thus will [^/] match all characters BUT a slash.

You can read more about regular expressions and meta characters at http://www.regular-expressions.info/reference.html