Solved

What is the best way to Regex this?

Posted on 2015-01-18
4
72 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

810 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