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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:21+00:00 2026-05-26T08:44:21+00:00

I am having problems with the nested ‘+’/’-‘ lookahead/lookbehind in regex. Let’s say that

  • 0

I am having problems with the nested ‘+’/’-‘ lookahead/lookbehind in regex.

Let’s say that I want to change the '*' in a string with '%' and let’s say that '\' escapes the next character. (Turning a regex to sql like command ^^).

So the string

  • '*test*' should be changed to '%test%',
  • '\\*test\\*' -> '\\%test\\%', but
  • '\*test\*' and '\\\*test\\\*' should stay the same.

I tried:

(?<!\\)(?=\\\\)*\*      but this doesn't work
(?<!\\)((?=\\\\)*\*)    ...
(?<!\\(?=\\\\)*)\*      ...
(?=(?<!\\)(?=\\\\)*)\*  ...

What is the correct regex that will match the ‘*’s in examples given above?

What is the difference between (?<!\\(?=\\\\)*)\* and (?=(?<!\\)(?=\\\\)*)\* or if these are essentially wrong the difference between regex that have such a visual construction?

  • 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-26T08:44:22+00:00Added an answer on May 26, 2026 at 8:44 am

    To find an unescaped character, you would look for a character that is preceded by an even number of (or zero) escape characters. This is relatively straight-forward.

    (?<=(?<!\\)(?:\\\\)*)\*        # this is explained in Tim Pietzcker' answer
    

    Unfortunately, many regex engines do not support variable-length look-behind, so we have to substitute with look-ahead:

    (?=(?<!\\)(?:\\\\)*\*)(\\*)\*  # also look at ridgerunner's improved version
    

    Replace this with the contents of group 1 and a % sign.

    Explanation

    (?=           # start look-ahead
      (?<!\\)     #   a position not preceded by a backslash (via look-behind)
      (?:\\\\)*   #   an even number of backslashes (don't capture them)
      \*          #   a star
    )             # end look-ahead. If found,
    (             # start group 1
      \\*         #   match any number of backslashes in front of the star
    )             # end group 1
    \*            # match the star itself
    

    The look-ahead makes sure only even numbers of backslashes are taken into account. Anyway, there is no way around matching them into a group, since the look-ahead does not advance the position in the string.

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

Sidebar

Related Questions

I'm having a myriad of problems with Xcode 4 and nested projects that worked
I'm having a problem with nested model forms that contain radio buttons, when I
im having problems starting a codeigniter project, the problem is that when i do
I'm having problems using textures that are larger than the OpenGL window or the
I have a couple of basic Rails problems that I'm having trouble finding relevant
I'm having problems with updating a child model via nested form. I've read many
I am still having problems with nested forms. Here is my form code: <%=
We're having a problem combining 'nested files' and excluding files from source control. We're
Having problems with a small awk script, Im trying to choose the newest of
Having problems iterating. Problem has to do with const correctness, I think. I assume

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.