【问题标题】:Stuck with regular expression卡在正则表达式中
【发布时间】:2014-04-04 04:11:35
【问题描述】:

来自另一个答案,我被一个正则表达式困住了(几率是多少......?)。

$matches    = array();
// $controller = $this->getRequest()->attributes->get('_controller');
$controller = "Acme\MyBundle\Controller\MyController::myAction";
preg_match('/(.*)\\\Bundle\\\(.*)\\\Controller\\\(.*)Controller::(.*)Action/', $controller, $matches);

print_r($matches);

返回 (see example)

Array
(
)

预期结果

Array
(
    [0] => Acme\MyBundle\Controller\MyController::myAction
    [1] => Acme
    [2] => My
    [3] => My
    [4] => my
)

有人可以帮忙吗?这个正则表达式似乎是合法的,也许它只是反斜杠的问题?我试过了,但没有得到它。

【问题讨论】:

  • 你想匹配什么?
  • 它与\MyBundle\ 部分不匹配,因为您的正则表达式中只有\Bundle\
  • @ShankarDamodaran 呃,是的,抱歉,忘了详细说明,会更新。

标签: php regex


【解决方案1】:

请尝试以下表达式。是预期的吗?或者告诉我你的具体要求。

<?php

$matches    = array();
// $controller = $this->getRequest()->attributes->get('_controller');
$controller = "Acme\MyBundle\Controller\MyController::myAction";
preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller::(.*)Action/', $controller, $matches);

print_r($matches);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多