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 8569259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:17:32+00:00 2026-06-11T18:17:32+00:00

I am trying to replace all occurrences of ANY value between two single quotes,

  • 0

I am trying to replace all occurrences of ANY value between two single quotes, including escaped single quotes. The following works quite well, except when an escaped single quote is found. This makes sense, but is there a way around it. I want to replace ALL characters between single quotes.

$ echo "'blah\'blah'" | perl -pe s/"'"[^"'"]*"'"/stuff/g
stuffblah'

I would like to just see:

stuff
  • 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-06-11T18:17:34+00:00Added an answer on June 11, 2026 at 6:17 pm

    Inside of our quotes, we want to allow the following tokens

    1. all non-quote characters [^'] or
    2. all escaped quotes \', encoded as \\' or
    3. all escaped backslashes \\, encoded as \\\\. We need this extra rule to represent all possible data.

    The or in regexes is denoted by the pipe character |, and we bracket these possible tokens with parens to group them for our iteration:

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

    or perhaps more clearly showing what can be escaped:

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

    Now we just pack this into quotes, and make the grouping parens non-saving (no variables $1 etc need to be created), and we have

    s {' (?: \\[\\'] | [^'] )* '}
      {stuff}gx
    

    Note the use of the /x modifier to include non-semantic whitespace for better readability.

    Edit: Perl one-liners

    All one-liners were tested unter bash. Other shells will display other quoting horrors.
    In the next line, the unescaped regex is provided.

    This one-liner matches single quotes, as specified:

    perl -pe"s/'(?:\\\\[\\\\']|[^'])*'/stuff/g"
             s/'(?:\\  [\\']  |[^'])*'/stuff/gx
    

    doubling the backslashes is done to accomodate for shell escaping.

    This one-liner matches double quotes:

    perl -pe"s/\"(?:\\\\[\\\\\"]|[^\"])*\"/stuff/g"
             s/ "(?:\\  [\\"]   |[^"] )* "/stuff/gx
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to write a regexp to replace all occurrences of \n with
I am trying to replace all occurrences of of a pattern in JavaScript. My
I'm trying to replace all the occurrences of a variable in a string using
I am trying to replace all the occurrences of \n in the Text property
Goal: Using jQuery, I'm trying to replace all the occurrences of: <code> ... </code>
I am trying to replace all the characters between an html font tag with
I'm trying to use jQuery to replace all occurrences of a particular string that
I'm trying to replace all occurrences of -> with . . I've tried the
Trying to replace all occurrences of an @mention with an anchor tag, so far
Possible Duplicate: php preg_replace \ I am trying to replace all occurances of the

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.