Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    quick regex question

    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():
    Code:
    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:
    Code:
    <strong>Bedrooms: </strong>4</td>
    or
    <strong>Bedrooms: </strong> </td>
    Can someone tell me how l'd go about matching either a #, or just a blank space?

    Thank you!
    Last edited by Coastal Web; 03-15-2010 at 08:08 PM.

  • #2
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    \s = space
    does # not match literally in the implementation you are using? If not try escaping it \#

  • #3
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    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:
    Code:
    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)
    ([0-9|?\s])
    ([0-9?\s]) ....

  • #4
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    This regex string works for me on a basic test - but check it does what you want:

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

  • #5
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Hi again 120, thanks!
    Just so l understand correctly here...
    PHP Code:
    '/<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
    Correct?
    '

  • #6
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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 ;-)

  • #7
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    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:
    Code:
    <strong>Bathrooms: </strong>2</td>
    <strong>Bathrooms: </strong>4</td>
    <strong>Bathrooms: </strong> </td>
    <strong>Bathrooms: </strong>1</td>
    returns:
    Code:
    array('2', '4', '1');
    instead here's what l'd want:
    Code:
    array('2', '4', ' ', '1')

  • #8
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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....

  • #9
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    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!)

  • #10
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    Slight modification to the regex to just return the item you want into the array tested with PHP:

    PHP Code:
    <?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>
    EOF;
    $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";
    }
    ?>
    OUTPUT
    Code:
    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 ;-)
    Last edited by 120; 03-15-2010 at 08:02 PM.

  • Users who have thanked 120 for this post:

    Coastal Web (03-15-2010)

  • #11
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Great thanks!

    PHP Code:
    preg_match_all('/<strong>Bedrooms: <\/strong>([0-9 ]*)<\/td>/ims'$content$beds); 
    ([0-9 ]*) seems to do the trick!

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    This one should be a tad more selective.

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

  • Users who have thanked MattF for this post:

    120 (03-15-2010)

  • #13
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    Better still :-) - only returns the actual match to the array[0]
    Code:
    (?<=<strong>Bedrooms: <\/strong>)([\d\s]+?)(?=<\/td>)

  • Users who have thanked 120 for this post:

    Coastal Web (03-24-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •