在RegEx

时间:2018-02-01 03:01:30

标签: php regex

很抱歉,因为我是RegEx的新手。我想在此字符串中找到括号内的所有连字符

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

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

括号 之外的连字符不得匹配 (例如,字符串中第一次出现连字符(..ter->get..)并且我&#39我很难解决这个问题。

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 个答案:

答案 0 :(得分:2)

对于您提供的示例字符串,

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

Click for Demo

<强>解释

  • - - 匹配-
  • (?=[^()]*\)) - 积极向前看,以确保上述步骤中匹配的-必须后跟关闭),而不会遇到(或{{{ 1}}之前。

注意:如果您有嵌套的括号,这将无法正常工作。

答案 1 :(得分:0)

r'\(.*?(-).*?\)' 假设php中的正则表达式与python中的正则表达式相同。请注意,外部括号用\ n进行转义,内部表示一个组