Skip to content

[Router] allow to use \A and \z as regex start and end #37711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

zlodes
Copy link
Contributor

@zlodes zlodes commented Jul 30, 2020

Q A
Branch? master
Bug fix? yes
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#...

Some people are using \A and \z as alternative for ^ and $ for route requirements.

E.g.: UUID pattern in ramsey/uuid (many routes broke after updating the lib):
ramsey/uuid@569e93a#diff-23c8536f7d61e7d839fd1c93ce0dd354L30-R31

References:
https://www.rexegg.com/regex-anchors.html#A
https://www.rexegg.com/regex-anchors.html#z

P.S.: It is my first PR into Symfony. Maybe I should fix something, just let me know.

@zlodes zlodes changed the title Router: allow to use \A and \z as regex start and end [Router] allow to use \A and \z as regex start and end Jul 30, 2020
@zlodes zlodes force-pushed the route-requirement-with-alternative-regex-start-and-end-syntax branch 2 times, most recently from 9892b31 to 9362fa8 Compare July 30, 2020 15:23
@zlodes
Copy link
Contributor Author

zlodes commented Jul 30, 2020

Travis failed with

fatal: couldn't find remote ref 4

@@ -546,6 +546,9 @@ public function compile()

private function sanitizeRequirement(string $key, string $regex)
{
// Replace \A, \z with classic ^ and $
$regex = str_replace(['\A', '\z'], ['^', '$'], $regex);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to replace all occurrences, only the ones on the edges should be dealt with.
For \A it's trivial, but for \z, we should deal with escaped backslashes, eg \\z should not be replaced.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Done. I also removed the type-casting for substr result because after checking for empty string substr($regex, 1) will always return string.

@nicolas-grekas nicolas-grekas added this to the next milestone Jul 30, 2020
@nicolas-grekas
Copy link
Member

Note that this could be considered as a new feature. It should target master.

@zlodes zlodes changed the base branch from 5.0 to master July 30, 2020 16:51
@zlodes zlodes force-pushed the route-requirement-with-alternative-regex-start-and-end-syntax branch from 9362fa8 to 9bf388d Compare July 30, 2020 16:54
@zlodes zlodes marked this pull request as draft July 30, 2020 16:59
@zlodes zlodes marked this pull request as ready for review July 30, 2020 17:10
@fabpot fabpot force-pushed the route-requirement-with-alternative-regex-start-and-end-syntax branch from 8430e81 to f752eee Compare July 31, 2020 06:30
@fabpot
Copy link
Member

fabpot commented Jul 31, 2020

Thank you @zlodes.

@fabpot fabpot merged commit bd59105 into symfony:master Jul 31, 2020
@rvitaliy
Copy link
Contributor

@nicolas-grekas @zlodes
we probably should add \Z too.
see pcre documentation: https://www.pcre.org/original/doc/html/pcrepattern.html#SEC5

\A matches at the start of the subject
\Z matches at the end of the subject also matches before a newline at the end of the subject
\z matches only at the end of the subject

@zlodes zlodes deleted the route-requirement-with-alternative-regex-start-and-end-syntax branch July 31, 2020 10:39
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
@fabpot fabpot mentioned this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants