rspec/rules/S6394/php/rule.adoc
2022-02-04 16:28:24 +00:00

45 lines
1.3 KiB
Plaintext

In PHP, PCRE functions, such as `preg_match`, are often used to perform regular expression operations. It is necessary to enclose the actual pattern with delimiters. The delimiters are usually `/`. Every occurrence of the delimiter character in the pattern has to be escaped with a backslash.
However, delimiters may also be any non-alphanumeric, non-backslash or non-whitespace character.
In addition to two identical delimiters, it is also allowed to use bracket style delimiters with the opening and closing brackets. Using different delimiters out of these possibilities allows avoiding unnecessary escaping, and increases the readability.
== Noncompliant Code Example
[source,php]
----
"/https:\/\/w{3}/i"
----
== Compliant Solution
[source,php]
----
"#https://w{3}#i"
"<https://w{3}>i"
----
== See
* https://www.php.net/manual/en/regexp.reference.delimiters.php[Delimiters] - PHP Documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Primary location: Use '%s' as delimiters to avoid escaping.
Secondary location: OAvoidable escaping.
=== Highlighting
Primary location on the entire regex.
Secondary location on each escaped character which is equal to delimiter.
'''
endif::env-github,rspecator-view[]