Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6318469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:38:28+00:00 2026-05-24T15:38:28+00:00

Below is a PHP regex intended to match (multiline) strings inside PHP or JavaScript

  • 0

Below is a PHP regex intended to match (multiline) strings inside PHP or JavaScript source code (from this post), but I suspect it’s got issues.
What is the literal Python (or else PERL) equivalent of this?

~'(\\.|[^'])*'|"(\\.|[^"])*"~s
  • the s modifier means dot matches all characters, including newline; in Python that’s re.compile(..., re.DOTALL)
  • I totally don’t get the intent of the leading \\. ? Does that reduce to . ? Are double-backslashes need to escape it twice in PHP?
  • allowing in every position a match of either \\. or [^'] (any non-quote character) seems total overkill to me, maybe explains why this person’s regex blows up. Does [^'] group not already match everything that . with s modifier does, surely it should match newlines?

  • for constructing two versions of the regex with single, and double, quotes in Python, can use this two-step approach

  • NB a simpler version of this regex can also be found in this list of PHP regex examples, under Programming: String.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:38:29+00:00Added an answer on May 24, 2026 at 3:38 pm

    The regex is mostly okay, except it doesn’t handle escaped quotes (i.e., \" and \'). That’s easy enough to fix:

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

    That’s a “generic” regex; in Python you would usually write it in the form of a raw string:

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

    In PHP you have to escape the backslashes to get them past PHP’s string processing:

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

    Most of the currently-popular languages have either a string type that requires less escaping, support for regex literals, or both. Here’s how your regex would look as a C# verbatim string:

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

    But, formatting considerations aside, the regex itself should work in any Perl-derived flavor (and many other flavors as well).


    p.s.: Notice how I added the + quantifier to your character classes. Your intuition about matching one character at a time is correct; adding the + makes a huge difference in performance. But don’t let that fool you; when you’re dealing with regexes, intuition seems to wrong more often than not. :/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I convert the below PHP code to VB.NET? <?php $X_HOST =foo.com; $X_URL
My PHP code below shows an image on browser as its original size 300x300.
When I use a conditional statement targeting IE6 and below with some PHP code
What is the simplest, fastest way to complete the PHP code below such that
The script below, test.php, is intended to be placed in a specific directory of
I have the following Regex used to match PHP Class, method and Function names
Below is a regex which works fine in .net but give me error if
I am trying to understand this one line of code below: str_replace('../', '', $route);
I'm trying to include this Regex condition and activate the analytics code (if it
I have URLs like below that need to be recognized by the PHP code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.