Translate the intent of this PHP regular expression for multi-line strings in Python / PERL

Below is a PHP regular expression designed to match (multi-line) strings inside PHP or JavaScript source code (from this post ), but I suspect it is having problems. What is the literal equivalent of Python (or PERL)?

~'(\\.|[^'])*'|"(\\.|[^"])*"~s
  • s modifier means that the period matches all characters, including the newline; in python whatre.compile(..., re.DOTALL)
  • I completely do not understand the intention of the leader \\.? Does it reduce to .? Do double backslashes need to be avoided twice in PHP?
  • allowing in each position a coincidence either \\., or [^'](any character without quotes) seems like a complete excess to me, maybe it explains why this human regex explodes. [^']Does the group not correspond to all that .with the s-modifier does, of course, it must match the newline characters?

  • you can use this two-step approach to build two versions of a regex with single and double quotes

  • NB A simpler version of this regular expression can also be found in this list of PHP regular expression examples in the Programming: String section .

+1
2

, , (.. \" \'). :

'(?:\\.|[^'\\]+)*'|"(?:\\.|[^"\\]+)*"

"" ; Python :

r"""'(?:\\.|[^'\\]+)*'|"(?:\\.|[^"\\]+)*""""

PHP , PHP:

'~\'(?:\\\\.|[^\'\\\\]+)*\'|"(?:\\\\.|[^"\\\\]+)*"~s'

, , , , . , #:

@"'(?:\\.|[^'\\]+)*'|""(?:\\.|[^""\\]+)*"""

, , Perl- ( ).


p.s.: , + . ; + . ; , , .:/

+1

\\. . : PHP ( Python) , \\\\. , \\. .

, , .

, , .

Python ( , re.DOTALL). Python , , . :

re.search(r'\'(\\.|[^\'])*\'|"(\\.|[^"])*"', str, re.DOTALL)

+2

All Articles