Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Simple problem driving me mad!

Last post 10-10-2008, 3:23 PM by prometheuzz. 11 replies.
Sort Posts: Previous Next
  •  10-10-2008, 1:35 PM 47081

    Simple problem driving me mad!

    Hey,

    Im trying to match the first part of an address,

    e.g: 
    22b street name
    92 street name

    This is what i have so far....

    $address = "22b; Broad St; Staple Hill";
    //$address = "22; Broad St; Staple Hill";

    $addressLine = explode(";",$address);
           

    if(preg_match("^[0-9]+([a-zA-Z])^", $addressLine[0]))
    {
      echo '<b>address line 1 has been matched as</b> '.$addressLine[0];
    }
    else
    {
      echo 'did not matched as '.$addressLine[0];
    }

     Problem is, its matching 22b, but not just 22.

     

    Any help greatly appreciated!

    Daz

  •  10-10-2008, 2:01 PM 47082 in reply to 47081

    Re: Simple problem driving me mad!

    Remove the second ^ in your pattern, if desired to match end of string replace the second ^ with a $.


  •  10-10-2008, 2:17 PM 47085 in reply to 47082

    Re: Simple problem driving me mad!

    I tried your suggestion, but get this error

    Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /home/public_html/test.php on line 8
    did not match as 22

  •  10-10-2008, 2:24 PM 47086 in reply to 47085

    Re: Simple problem driving me mad!

    I see you also left off the delimiters, it was interpreting your ^ as delimiters.

     

    if(preg_match("/^[0-9]+([a-zA-Z])/", $addressLine[0]))

    or

    if(preg_match("/^[0-9]+([A-Z])/i", $addressLine[0]))

    see:

    http://us2.php.net/manual/en/function.preg-match.php


  •  10-10-2008, 2:38 PM 47087 in reply to 47086

    Re: Simple problem driving me mad!

    thanks for the heads up.

     I tried what you suggested, but it still wont match "22"

    I have reviewed that php.net link but it doesnt explain about the specific regex im stuck on.

    So far i have this....

    if(preg_match("/^[0-9]+([a-zA-Z])/", $addressLine[0]))

    It matches 22b.........but not 22 by itself.........any ideas please?

  •  10-10-2008, 2:44 PM 47088 in reply to 47087

    Re: Simple problem driving me mad!

    ----------------------------------------------------------------------
      ^                        the beginning of the string
    ----------------------------------------------------------------------
      [0-9]+                   any character of: '0' to '9' (1 or more
                               times (matching the most amount possible))
    ----------------------------------------------------------------------
      (                        group and capture to \1:
    ----------------------------------------------------------------------
        [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
    ----------------------------------------------------------------------
      )                        end of \1
    ----------------------------------------------------------------------
    If you want to make the
    ([a-zA-Z]) optional you will need add a ? to the end:

    if(preg_match("/^[0-9]+([a-zA-Z])?/", $addressLine[0]))


  •  10-10-2008, 2:46 PM 47089 in reply to 47087

    Re: Simple problem driving me mad!

    if(preg_match("/^([0-9]+)([a-zA-Z])/",$addressLine[0],$res)){

    echo '<pre>'.print_r($res,true);

    }


  •  10-10-2008, 2:46 PM 47090 in reply to 47087

    Re: Simple problem driving me mad!

    fulleffect:

    thanks for the heads up.

     I tried what you suggested, but it still wont match "22"

    I have reviewed that php.net link but it doesnt explain about the specific regex im stuck on.

    So far i have this....

    if(preg_match("/^[0-9]+([a-zA-Z])/", $addressLine[0]))

    It matches 22b.........but not 22 by itself.........any ideas please?

    Make the a-zA-Z reluctant:

    /^[0-9]+[a-zA-Z]?/

  •  10-10-2008, 2:52 PM 47091 in reply to 47090

    Re: Simple problem driving me mad!

    thanks for your help ddrudik !!

    prometheuzz: What do you mean by reluctant?

    I understand what you've changed, but i dont know why?

     

    ([a-zA-Z])   versus    [a-zA-Z]     ? 

  •  10-10-2008, 3:00 PM 47093 in reply to 47091

    Re: Simple problem driving me mad!

    fulleffect:

    thanks for your help ddrudik !!

    prometheuzz: What do you mean by reluctant?

    I understand what you've changed, but i dont know why?

    Err, you understand, but you don't understand?

    fulleffect:

    ([a-zA-Z])   versus    [a-zA-Z]     ? 

    The parenthesis are for grouping characters, I didn't see you use the grouping, so I left them out. More info: http://www.regular-expressions.info/brackets.html

    And the '?' is a reluctant/optional/un-greedy quantifier. More info: http://www.regular-expressions.info/optional.html

  •  10-10-2008, 3:03 PM 47094 in reply to 47093

    Re: Simple problem driving me mad!

    thanks again prometheuzz!

    This now works perfectly!

    Although, if i change the address to   22bbb, it also matches.

    How would i limit that to just 1 letter after the number?

    I have tried  if(preg_match("/^[0-9]+[a-zA-Z]{1}?/", $addressLine[0]))

     But it still matches more than 1 letter?

  •  10-10-2008, 3:23 PM 47095 in reply to 47094

    Re: Simple problem driving me mad!

    That's because your regex will look for text starting with a number followed by an optional letter. The string "12", "12b" and "12bxc" all match that criteria. Try something like this:

    /^\d++[a-zA-Z]?\s/

    The \s in the regex denotes a white space character.

View as RSS news feed in XML