Solved

What is the best way to Regex this?

Posted on 2015-01-18
4
69 Views
Last Modified: 2015-01-18
I need to Regex this statement:  What is the easiest way to accomplish?

Pro Audio > Audio Recording > Drum Machines/Samplers > Alesis SR18 Portable High-Definition

I need:
Pro Audio
Audio Recording
Drum Machines/Samplers

<span class="head2"><strong><a href="category/pro-audio/1"><span class="head2">Pro Audio</span></a> </strong></span><strong> <span class="head3">&gt;</span> <a href="category/pro-audio/audio-recording/1">Audio Recording</a>

Open in new window



My attempt which did not work...
<span class="head2"><strong><a href="category\/.*?\/1"><span class="head2">(.*?)<\/span><\/a><\/strong><\/span><strong>\s<span class="head3">&gt;<\/span>\s<a href="category\/.*?\/.*?\/1">(.*?)<\/a>\s+\&gt;\s<a href="products\/.*?\/.*?\/.*?/1">(.*?)<\/a> <\/span>\s+\&gt;\s(.*?)\s+<\/span>\s+<\/strong>\s+<\/td>

Open in new window



preg_match('%<span class="head2"><strong><a href="category\/.*?\/1"><span class="head2">(.*?)<\/span><\/a><\/strong><\/span><strong>\s<span class="head3">&gt;<\/span>\s<a href="category\/.*?\/.*?\/1">(.*?)<\/a>\s+\&gt;\s<a href="products\/.*?\/.*?\/.*?/1">(.*?)<\/a> <\/span>\s+\&gt;\s(.*?)\s+<\/span>\s+<\/strong>\s+<\/td>%si',$buffer4,$matches4);

$a=$matches4[1];
$b=$matches4[2];
$c=$matches4[3];
$d=$matches4[4];


echo "a:&nbsp;&nbsp;".$a."<BR>";
echo "b:&nbsp;&nbsp;".$b."<BR>";
echo "c:&nbsp;&nbsp;".$c."<BR>";
echo "d:&nbsp;&nbsp;".$d."<BR>";

Open in new window

0
Comment
Question by:lawrence_dev
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40556943
<\/a><\/strong>
should be
<\/a>\s*<\/strong>
or
</a>\s+</strong>

I also don't see the part of
<span class="head2"><strong><a href="category/pro-audio/1"><span class="head2">Pro Audio</span></a> </strong></span><strong> <span class="head3">&gt;</span> <a href="category/pro-audio/audio-recording/1">Audio Recording</a>
that's supposed to match \&gt;\s<a href="products
0
 
LVL 45

Accepted Solution

by:
aikimark earned 350 total points
ID: 40556962
Try this:
<span class="head2"><strong><a href="category/.*?/1"><span class="head2">(.*?)</span></a>\s*</strong></span><strong>\s<span class="head3">&gt;</span>\s<a href="category/.*?/.*?/1">(.*?)</a>(?:\s+\&gt;\s<a href="products/.*?/.*?/.*?/1">(.*?)</a> </span>\s+\&gt;\s(.*?)\s+</span>\s+</strong>\s+</td>)?

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40556986
Thanks Aikimark!!  I am half way there.  I played with it a little and could not make it work.

Notice: Undefined offset: 3 on line 281

Notice: Undefined offset: 4  on line 282
a:  Pro Audio
b:  Live Sound
c:  
d:
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 150 total points
ID: 40556990
What was in the original string that should have matched offset: 3 and offset:4?
There was nothing in
http://www.experts-exchange.com/viewCodeSnippet.jsp?refID=28599127&rtid=10&icsi=1
that would have produced anything for c: and d:
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mail Not Sent 6 43
Generating a set of random unique numbers 6 19
Paging Using PHP 7 33
How to fetch your row in php 14 8
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now