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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:33:36+00:00 2026-05-15T18:33:36+00:00

First things first. I know how to parse XML/HTML with simplexml, and I know

  • 0

First things first. I know how to parse XML/HTML with simplexml, and I know all the arguments against using RegEx to parse it. This question is for the sake of knowledge.

What needs to happen

In a block of text let’s say we have the following line of text:

The query you need to use is 
<code>SELECT `post_name` FROM table WHERE id= $id</code> 
where `$id` is the `user_ID` we got earlier.

How do you match the following:

 `$id`
 `user_ID`

without also matching

`post_name`?

Requirements

This needs to be a regex-only solution. I understand and know how to use things like preg_replace_callback etc to remove <code> blocks from the string first, but I’m looking for a regex-only solution. Also, it needs to be able to handle possible attributes like<code lang="php">.

The regex needs to match pairs of backticks that are not between <code> and </code> and the matches may not contain either <code> or </code> to handle lone backticks in other contexts.

The content in the backticks will never be multi-lined.

Reasoning

I’m working on a personal project where this was a possible edge-case. This is not a mark-down type project where it is possible to change the order of the calls. The <code> tags are in the source text and not going anywhere.

Also, part of the reason I don’t want “use simpleXML” answers is because the backticks are not inside actual <code> blocks. It is just a handy way to explain the problem and the solution for <code> blocks will work with slight changes.

  • 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-15T18:33:36+00:00Added an answer on May 15, 2026 at 6:33 pm

    I don’t think regular expressions are a good tool for this, but it can be done if you assume that the code tags aren’t nested:

    `(?:(?!</?code>)[^`])*`(?!(?:(?!<code>).)*</code>)
    

    This means:

    `(?:(?!</?code>)[^`])*`       : Match something in backticks unless it
                                    contains <code> or </code> or a backtick...
    (?!(?:(?!<code>).)*</code>)   : unless it is followed by a </code>
                                    without a <code> first.
    

    See the regular expression in action at rubular.

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

Sidebar

Ask A Question

Stats

  • Questions 536k
  • Answers 536k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should not use the pointer sign (@) at the… May 17, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer The problem is probably event bubbling.. what you could do,… May 17, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer EricLaw's page maintains a list of supporting browsers. Current verions… May 17, 2026 at 1:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In case of things that are hard to test using traditional xUnit-style methods, such
One of the first things I learned as a web developer was to never
I intend to implement Martin Fowler's Two-Step View Pattern for rendering HTML in a
I'm generating regular expressions dynamically by running through some xml structure and building up
I am very new to XSLT, and the first thing that i need to
First off here is my code. IEnumerable<XElement> targetDirectory = from XElement e in workingXmlDocument.Descendants(wixNS
Okay, I've written my first functional PHP extension. It worked but it was a
I have a pretty simple question (and these are typically the ones I spend
Before anything, let me first clarify that the below thoughts are purely my personal
Hey guys, I have been programming with as3 for the past 4 months and

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.