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

  • SEARCH
  • Home
  • 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 1042345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:29:39+00:00 2026-05-16T15:29:39+00:00

I am trying to remove quotes from a string. Example: hello, how ‘are you

  • 0

I am trying to remove quotes from a string. Example:

"hello", how 'are "you" today'

returns

hello, how are "you" today

I am using php preg_replace.

I’ve got a couple of solutions at the moment:

(\'|")(.*)\1

Problem with this is it matches all characters (including quotes) in the middle, so the result ($2) is

hello", how 'are "you today'

Backreferences cannot be used in character classes, so I can’t use something like

(\'|")([^\1\r\n]*)\1

to not match the first backreference in the middle.

Second solution:

(\'[^\']*\'|"[^"]*")

Problem is, this includes the quotes in the back reference so doesn’t actually do anything at all. The result ($1):

"hello", how 'are "you" today'
  • 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-16T15:29:39+00:00Added an answer on May 16, 2026 at 3:29 pm

    Instead of:

    (\'[^\']*\'|"[^"]*")
    

    Simply write:

    \'([^\']*)\'|"([^"]*)"
      \______/    \_____/
         1           2
    

    Now one of the groups will match the quoted content.

    In most flavor, when a group that failed to match is referred to in a replacement string, the empty string gets substituted in, so you can simply replace with $1$2 and one will be the successful capture (depending on the alternate) and the other will substitute in the empty string.

    Here’s a PHP implementation (as seen on ideone.com):

    $text = <<<EOT
    "hello", how 'are "you" today'
    EOT;
    
    print preg_replace(
      '/\'([^\']*)\'|"([^"]*)"/',
      '$1$2',
      $text
    );
    # hello, how are "you" today 
    

    A closer look

    Let’s use 1 and 2 for the quotes (for clarity). Whitespaces will also be added (for clarity).

    Before, you have, as your second solution, this pattern:

    (  1[^1]*1  |  2[^2]*2  )
    \_______________________/
       capture whole thing
       content and quotes
    

    As you correctly pointed out, this match a pair of quotes correctly (assuming that you can’t escape quotes), but it doesn’t capture the content part.

    This may not be a problem depending on context (e.g. you can simply trim one character from the beginning and end to get the content), but at the same time, it’s also not that hard to fix the problem: simply capture the content from the two possibilities separately.

    1([^1]*)1  |  2([^2]*)2
     \_____/       \_____/
     capture contents from
    each alternate separately
    

    Now either group 1 or group 2 will capture the content, depending on which alternate was matched. As a “bonus”, you can check which quote was used, i.e. if group 1 succeeded, then 1 was used.


    Appendix

    The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.

    (…) is used for grouping. (pattern) is a capturing group and creates a backreference. (?:pattern) is non-capturing.

    References

    • regular-expressions.info/Brackets for capturing, Alternation, Character class, Repetition
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove quotes from something like: Hello so that the string
I'm running asp.net MVC razor. I'm trying to remove quotes from a string in
I am trying to remove the slashes from magic quotes from an array. So
Trying to remove single quotes from around numbers. I'm working with third paty data
I was trying to remove index.php from the URL of a Magento website: _Turn
I'm trying to remove part of the path in a string. I have the
I'm trying to remove scrollbar from my facebook app. I tried js solution, that
I'm trying to remove wiki formatting from some text so it can be parsed.
I'm trying to remove a specific graph object from the zedgraph, is it possible....?
I am trying to remove everything between curly braces in a string, and trying

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.