Skip to main content
December 29, 2009
Answered

Regexp help

  • December 29, 2009
  • 1 reply
  • 595 views

Let’s say I have a regexp that searches through some content and returns everything between bold tags (<b></b>).  For example: ‘/<b>(.*?)</b>/’

Now, let’s say that I want to add more than the bold tags – like the <h1> tags.

Can I incorporate that into the first regexp?  Or, do I have to do separate regexps?

I thought of just adding in the new requirement like ‘/(<b>(.*?)</b>,<h1>(.*?)</h1>)/’ – but received more errors then I like to talk about.

There has to be a simple way to combine these or add several together without having to do separate regexp for each.

Most manuals and tutorials simply show what can be matched but not really how to combine them.

This topic has been closed for replies.
Correct answer David_Powers

The following regex will match <strong>, <h1>, <h2>, and <em> tags:

<(strong|h1|h2|em)>(.*?)<\/\1>

The \1 is a backreference to the match in the first set of parentheses. The vertical pipe (|) designates alternatives.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
December 30, 2009

The following regex will match <strong>, <h1>, <h2>, and <em> tags:

<(strong|h1|h2|em)>(.*?)<\/\1>

The \1 is a backreference to the match in the first set of parentheses. The vertical pipe (|) designates alternatives.

December 30, 2009

Thank you so very much.  You are very helpful.  I have searched all the online manuals and could not figure out how to combine them.  All of this is new to me but I am learning everyday.  Thanks for your help!

December 30, 2009

Having a little trouble with this (<(strong|h1|h2|em)>(.*?)<\/\1>).  Let’s say I have the following:

<strong>This is great</strong>

<p><h1>How did I ever do without this</h1></p>

My goal is to search for and match everything between the tags like:

This is great

How did I ever do without this

But, instead I am getting the tags themselves.

Thus, it is printing:

strong

h1

I went to the php manual and tried several things like putting all terms in () then using a back reference of \\2

I have tried to move terms around and even spell them all out like <strong>|<h1>|<h2>|<em> (.*?) <\/\strong>|<\/\h1>|<\/\h2>|<\/\em>

So far no luck.  I either get the tags themselves returned or nothing or it just prints blanks.

Is there something I am missing here?