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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:05:28+00:00 2026-06-15T17:05:28+00:00

So I am new to regular expressions and I am attempting to write one

  • 0

So I am new to regular expressions and I am attempting to write one that will allow me to replace an apostrophe (') with \'. The regex that I came up with worked with all my test cases when I tested it on http://myregextester.com, but completely error out when I implement it in my code.

Anyways, this is what I have as of yet:

preg_replace('/((?<!\\)\'+(?=\d\ds\b))|(\b(?<=\w)(?<!\\)\'+(?=\w+\b))/','\'',$text);

and it throws this error:

A PHP Error was encountered
Severity: Warning
Message: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 50

I have counted like twenty times to see where the ) is not matching up, and to my eyes there is not. Here is one of my several counts:

( ( ?<!\\ )'+ ( ?=\d\ds\b ) ) | ( \b ( ?<=\w ) ( ?<!\\ ) '+ ( ?=\w+\b )  )
1 2       3   4           5 6   7    8       9 10      11   12        13 14

Any ideas, or pointing out my glaringly obvious mistakes a newbie can’t spot would be much appreciated.

  • 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-15T17:05:29+00:00Added an answer on June 15, 2026 at 5:05 pm

    \\) in a string literal becomes \) – escaped closing parenthesis – in a regex pattern, as double backslash sequence is interpolated into a single backslash even within the string literal delimited by single quotation marks. The problem is, though, that \) sequence in regex pattern is used to represent a literal ) symbol (which will otherwise be parsed as a metacharacter).

    What you intend to do is probably best written with \\\\). This way each \\ sequence in a string literal becomes a single \ in the pattern. The pattern parser will see this:

    \\)

    … a literal backslash symbol, followed by ) metacharacter.

    But it seems to be there’s another problem here: in your regex you’re looking for some special (a series) of apostrophes, yet replace them with single apostrophe again. Remember, \' within a string literal is just that – single quotation mark! You probably meant to use '\\\'' here instead.

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

Sidebar

Related Questions

I'm new at regular expressions and wonder how to phrase one that collects everything
I'm new to Regular Expressions, please help me to write a new Regex. It
am new to regular expressions and i cant handle a regex search using grep..
I'm completely new to regular expressions, and I need to filter all the words
I am new to regular expressions and I need a regex for php username
I'm new to regular expressions in Java (or any language, for that matter) and
I'm pretty new to regular expressions. I have a requirement to replace spaces in
I'm new to regular expressions and would like to use one to search through
I'm new to regular expressions, and I need to write a set of regular
I am attempting to run a program that will find a new value for

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.