Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Regexp help

Guest
Dec 29, 2009 Dec 29, 2009

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.

TOPICS
Server side applications
597
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 29, 2009 Dec 29, 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.

Translate
LEGEND ,
Dec 29, 2009 Dec 29, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 30, 2009 Dec 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 30, 2009 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 30, 2009 Dec 30, 2009

brywilson88 wrote:

Is there something I am missing here?

Yes, the regex does not need changing, but you have overlooked the fact that there are now two sets of capturing parentheses. So, assuming you're using this in your previous script, the value between the tags will be $search_results[2], not $search_results[1].

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 30, 2009 Dec 30, 2009
LATEST

Thank you.  Would have never thought of that - I thought I had something wrong with the regexp.  Thanks again - happy new year!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines