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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:53:16+00:00 2026-05-27T11:53:16+00:00

I have this block of html: <div> <p>First, nested paragraph</p> </div> <p>First, non-nested paragraph.</p>

  • 0

I have this block of html:

<div>
  <p>First, nested paragraph</p>
</div>
<p>First, non-nested paragraph.</p>
<p>Second paragraph.</p>
<p>Last paragraph.</p>

I’m trying to select the first, non-nested paragraph in that block. I’m using PHP’s (perl style) preg_match to find it, but can’t seem to figure out how to ignore the p tag contained within the div.

This is what I have so far, but it selects the contents of the first paragraph contained above.

/<p>(.+?)<\/p>/is

Thanks!

EDIT

Unfortunately, I don’t have the luxury of a DOM Parser.

I completely appreciate the suggestions to not use RegEx to parse HTML, but that’s not really helping my particular use case. I have a very controlled case where an internal application generated structured text. I’m trying to replace some text if it matches a certain pattern. This is a simplified case where I’m trying to ignore text nested within other text and HTML was the simplest case I could think of to explain. My actual case looks something a little more like this (But a lot more data and minified):

#[BILLINGCODE|12345|11|15|2001|15|26|50]#
[ITEM1|{{Escaped Description}}|1|1|4031|NONE|15]
#[{{Additional Details }}]#
[ITEM2|{{Escaped Description}}|3|1|7331|NONE|15]
[ITEM3|{{Escaped Description}}|1|1|9431|NONE|15]
[ITEM4|{{Escaped Description}}|1|1|5131|NONE|15]

I have to reformat a certain column of certain rows to a ton of rows similar to that. Helping my first question would help actual project.

  • 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-27T11:53:17+00:00Added an answer on May 27, 2026 at 11:53 am

    Your regex won’t work. Even if you had only non nested paragraph, your capturing parentheses would match First, non-nested ... Last paragraph..

    Try:

    <([^>]+)>([^<]*<(?!/?\1)[^<]*)*<\1>

    and grab \2 if \1 is p.

    But an HTML parser would do a better job of that imho.

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

Sidebar

Related Questions

i have this html code <div class=container id=container> <div class=post><h1>first title</h1>description here</div> <div class=post><h1>second
I have two DIV elements. HTML looks like this: <div id=first><div id=second></div></div> The CSS
I have this block of xaml that allows the text of a hyperlink to
I'm trying to have this block element to be horizontally aligned in the middle
Anyone have any experience using the Microsoft Configuration Management Application Block? This is an
I have a JSP page that renders a block of HTML. In normal usage,
I have this HTML: <div class=info> Some Text Boom A <a onclick=menu('id1');>Link</a> | More
I'm coding my first website using XML+XSL. The xml I am transforming have this
I have a block of HTML that I would like to use as the
I have the following html <div id=to-sort> <div class=block data-id=1>aaaaa</div> <div class=block data-id=2>bbbbb</div> <div

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.