Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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].
Copy link to clipboard
Copied
Thank you. Would have never thought of that - I thought I had something wrong with the regexp. Thanks again - happy new year!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more