1
votes

Sorry because I'm new to RegEx. I want to find all the hyphens inside the parentheses in this string:

$router->get('template-123s', 'Template123Controller@index')->name('template-123s');

$router->get('template-123s/{id}', 'Template123Controller@show')->name('get-template-123');

The hyphens outside the parenthesis must not be matched (for example, the first occurrence of the hyphen (..ter->get..) in the string and I'm having a hard time solving this problem.

Here is one of my attempts in trying to find the hyphens between the parenthesis, which is currently not working.

Here is a sample attempt of matching all the characters between the hyphens and it works.

2

2 Answers

2
votes

This would work for the example strings which you have provided:

-(?=[^()]*\))

Click for Demo

Explanation:

  • - - matches -
  • (?=[^()]*\)) - positive lookahead to make sure that the - matched in the above step must be followed by a closing ) without encountering either a ( or a ) before that.

NOTE: This would not work properly if you have nested parenthesis.

0
votes

r'\(.*?(-).*?\)' assuming regex in php is the same as in python. Note that outside parentheses are escaped with a \, and the insides signify a group