reguller expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

reguller expression

Post by itsmani1 »

from a big html i want i to fetch this:

Code: Select all

<ul class="linklist16">
  <li><a href="art.php?id=3">Camilla to undergo major operation</a></li>
  <li><a href="art.php?id=3741703">'SAS bid to free kidnapped Britons'</a></li>
  <li><a href="art.php?id=3761180">Big rise in 999 ambulance call-outs</a></li>
  <li><a href="art.php?id=3614991">Stephen Hawking to go weightless</a></li>
  <li><a href="art.php?id=3760313">New intervention over email report</a></li>
  <li><a href="art.php?id=3760314">Diana pre-inquest hearing under way</a></li>
  <li><a href="art.php?id=3745484">Zeebrugge victims remembered</a></li>
  <li><a href="art.php?id=3747337">Presidential hopefuls hit town</a></li>
  <li><a href="art.php?id=3691839">Tornadoes in the US kill 20</a></li>
</ul>
This i want to do with preg_match() function
I think my reguller expression can start like:

Code: Select all

^<ul class="linklist16">
but how to close it?
any help?

thank you
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

<ul class="linklist16">.*?<\/ul>

?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

tried this:

Code: Select all

$exp = "<ul class=.*?<\/ul>";
	echo preg_match($exp, $content);
but not working
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The pattern does not have start and end delimiters.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I'm sure ,this should work

Code: Select all

$pattern='/(<ul(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)+.*<\/ul>/si';
preg_match($pattern,$exp,$x);
print($x);
$x will get the result. The base expression is from a php net site.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

itsmani1: start and end delimiters are the / / or # # around most regular expressions
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

thank you,

I tried this code to catch data between <td width="8" class="page_l">&nbsp;</td> and <td width="15" valign="top" bgcolor="#f78222" class="wPage_r_tiler"> but its not working i don't know what's the problem with it.

Code: Select all

$pattern='/(<td width="8" class="page_l">&nbsp;</td>(.*)+.*<\/<td width="15" valign="top" bgcolor="#f78222" class="wPage_r_tiler">/si';
preg_match($pattern,$data,$x);
print($x);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

(.*)+.*<\/
should be

Code: Select all

(.*?)
Post Reply