View Full Version : Resolved quick regex question

Coastal Web
03-15-2010, 05:45 PM
Hi Guys, l'm trying to use regular expressions to match either a number 0-9, or space but can't figure it out.

For example here's my preg_match_all():

preg_match_all('/<strong>Bedrooms: <\/strong>([0-9])<\/td>/ims', $content[1], $bed);

Sometimes it's a #, but sometimes it's just a blank space.
For example:

<strong>Bedrooms: </strong>4</td>
<strong>Bedrooms: </strong> </td>

Can someone tell me how l'd go about matching either a #, or just a blank space?

Thank you!

03-15-2010, 06:48 PM
\s = space
does # not match literally in the implementation you are using? If not try escaping it \#

Coastal Web
03-15-2010, 06:51 PM
Hi 120,

Thanks for the response. I was able to find that \s would work but l'm not sure how to implement it.

Would l use:

preg_match_all('/<strong>Bedrooms: <\/strong>([0-9])?(\s)<\/td>/ims', $content[1], $bed);

To match, either a number or a blank space?
I've tried a few different variations but can't seem to figure it out.
([0-9?\s]) ....

03-15-2010, 06:59 PM
This regex string works for me on a basic test - but check it does what you want:

'/<strong>Bedrooms: <\/strong>(\d|#|\s)<\/td>/ims'

Coastal Web
03-15-2010, 07:03 PM
Hi again 120, thanks!
Just so l understand correctly here...

'/<strong>Bedrooms: <\/strong>(\d|#|\s)<\/td>/ims'

the \d matches digits (ie 0-9)
the # matches the pound sign? (l don't actually need to check for #, l said that meaning that l'm trying to match either a 0-9 number, or a blank space.
the \s matches the space.

So to remove the check for # and just match either 0-9 or a blanks pace it'd be:
'/<strong>Bedrooms: <\/strong>(\d|\s)<\/td>/ims

03-15-2010, 07:07 PM
Yep - that works for me, but I'm testing it on a simple regex tester under Linux - not using the PHP engine - so check it first ;-)

Coastal Web
03-15-2010, 07:10 PM
Hmmm, nope doesn't seem to work.
It either catches the digits (0-9) or nothing at all, so it's still missing the instances where

For example, running this preg_match_all() on:

<strong>Bathrooms: </strong>2</td>
<strong>Bathrooms: </strong>4</td>
<strong>Bathrooms: </strong> </td>
<strong>Bathrooms: </strong>1</td>


array('2', '4', '1');
instead here's what l'd want:

array('2', '4', ' ', '1')

03-15-2010, 07:15 PM
I'm not 100%, but with PHP don't you have to escape the escape? thus \ = \\
I'll look in my notes - hang on a minute....

03-15-2010, 07:24 PM
Nope, can't find my notes. I can promise you one thing - that regex works outside of PHP so there are a couple of 'possibles' here:

1. The PHP function has an issue with spaces {I seem to recall running into that before somewhere - hence looking for a note on it, but I'm not seeing anything}
2. You are getting a match, but your array is not being populated when space is matched.

Could you post your loop / iterator code chunk and I'll try it out for a fix (and make a bl**dy note of it this time!)

03-15-2010, 07:59 PM
Slight modification to the regex to just return the item you want into the array tested with PHP:

$testregex = <<<EOF
<strong>Bedrooms: </strong>#</td>
<strong>Bedrooms: </strong> </td>
<strong>Bedrooms: </strong> </td>
<strong>Bedrooms: </strong>0</td>
<strong>Bedrooms: </strong>55</td>
<strong>Bedrooms: </strong>5</td>
$spattern = "/(?<=<strong>Bedrooms: <\/strong>)(\d|#|\s)(?=<\/td>)/ims";
preg_match_all($spattern, $testregex, $output);

foreach ($output[0] as $key => $value) {
echo "Key: $key; Value: $value[0]<br />\n";


Key: 0; Value: #
Key: 1; Value:
Key: 2; Value: 0
Key: 3; Value: 5
Caveats - my php is weak , so this is probably not optimal plus I left the '#' match in for myself. The key regex works and does not return more than one digit or more than one space.
Hope that helps ;-)

Coastal Web
03-15-2010, 08:07 PM
Great thanks!

preg_match_all('/<strong>Bedrooms: <\/strong>([0-9 ]*)<\/td>/ims', $content, $beds);

([0-9 ]*) seems to do the trick!

03-15-2010, 08:36 PM
This one should be a tad more selective.

preg_match_all('#<strong>Bedrooms: </strong>([\d\s]+?)</td>#ims', $content, $beds);

03-15-2010, 09:10 PM
Better still :-) - only returns the actual match to the array[0]

(?<=<strong>Bedrooms: <\/strong>)([\d\s]+?)(?=<\/td>)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum