【问题标题】:Finding and matching a specific character between two characters in RegEx在 RegEx 中的两个字符之间查找和匹配特定字符
【发布时间】:2018-02-01 03:01:30
【问题描述】:

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

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

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

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

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.

【问题讨论】:

    标签: php regex


    【解决方案1】:

    适用于您提供的示例字符串

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

    Click for Demo

    说明:

    • - - 匹配 -
    • (?=[^()]*\)) - 正向前瞻,以确保在上述步骤中匹配的 - 必须后跟关闭 ),而不会在此之前遇到 ()

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

    【讨论】:

    • 不客气。如果它满足您的所有测试字符串,请考虑接受它作为答案
    • StackOverflow 说我必须等一分钟才能接受答案! >_
    【解决方案2】:

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

    【讨论】:

    • 对不起老兄,即使是 Python 风格也不能工作,不过还是谢谢! regex101.com/r/35DGxs/1
    • appologies,我忘了在开头添加原始字符串名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多