I am doing magento2 developer training. I am working on exercise"Create a new router which “understands” URLs like /frontNameactionPath- action and converts them to /frontName/actionPath/action". In that exercise I come across the code

if (preg_match("%^/(test)-(.*?)-(.*?)$%", $info, $m)) {
    $request->setPathInfo(sprintf("/%s/%s/%s", $m[1], $m[2], $m[3]));
    return $this->actionFactory->create('Magento\Framework\App\Action\Forward',
    ['request' => $request]);

I want to know, What is % in regular expression "%^/(test)-(.?)-(.?)$%"?


These are just the delimiters on the regular expression and not part of the expression itself. My thinking is they used % and not the normal / so that the / in the regex itself would not have to be escaped.

  • Why this delimiter used? is this optional?
    – Pandurang
    Apr 27 '17 at 9:26
  • 1
    @PMB I think they picked % over the "normal" / so as to avoid having to escape the / inside the expression itself. Apr 27 '17 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.