regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Reiter

    regular expression

    Hi,

    I need one function to validate streetnames and one to validate names.
    I tried to write my own regular expression and to use preg_match,
    but for some weird reason I can't get it to work.

    Valid names should be:
    Peter
    Christian-Peter
    Li
    Li Yan Young
    Li - Yan - Young
    Li- Yan -Young - Mei

    Valid streetnames should be:
    Jackrippperstr. 10a
    Jackrippperstr 9391 B
    Jack-The - Ripper Str. 3
    Jack -The- Ripper B
    Jack -The- Ripper

    Here are my regular expressions:
    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++
    /*/////////////
    ///1. Names ///
    /////////////*/

    //At least 2 chars:
    $twochars ="(A-Za-zßäüöÄÜÖ){2,}"; //Including german special chars
    // Separator with space and or -
    $separator= "([:space:]?-[:space:]?|[:space:])";
    /*
    (Name consists of at least 2 chars, optionally followed by one space
    char and or - , followed by again at least 2 chars )+
    */
    $name= "($twochars($se parator$twochar s)*)+";
    if ( preg_match("/$name/",$value) )

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++
    /*///////////////////////////
    ///2. Streetname + number ///
    ///////////////////////////*/


    //At least 2 chars:
    $twochars ="(A-Za-zßäüöÄÜÖ){2,}"; //Including german special chars
    // Separator with space and or -
    $separator= "([:space:]?-[:space:]?|[:space:])";
    // optionally Str. or Str or str or Str.
    $str = "([Ss]tr|[Ss]tr.)?";
    /*
    The numbers (1-9 followed by 0-9 ) - zero or four times, followed by
    space or not, optionally followed by a character.
    */
    $nr = "((1-9)+(0-9)*){0,4}[:space:]?(A-Za-z)?";

    $street= "($twochars($se parator$twochar s)*)+$str$nr";
    if ( preg_match("/$street/",$value) ) ...
    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++

    Have you any idea why this doesn't work?

    Thanks,

    Stefan
  • Justin Koivisto

    #2
    Re: regular expression

    Stefan Reiter wrote:
    [color=blue]
    > Here are my regular expressions:
    > +++++++++++++++ +++++++++++++++ +++++++++++++++ +++
    > /*/////////////
    > ///1. Names ///
    > /////////////*/
    >
    > //At least 2 chars:
    > $twochars ="(A-Za-zßäüöÄÜÖ){2,}"; //Including german special chars[/color]

    $twochars ='[A-Za-zßäüöÄÜÖ]{2,}';
    [color=blue]
    > // Separator with space and or -
    > $separator= "([:space:]?-[:space:]?|[:space:])";[/color]

    $separator= '[[:space:]-]?';
    [color=blue]
    > /*
    > (Name consists of at least 2 chars, optionally followed by one space
    > char and or - , followed by again at least 2 chars )+
    > */
    > $name= "($twochars($se parator$twochar s)*)+";[/color]

    $name = '^'.$twochars.' ('.$separator.$ twochars.')*$';
    [color=blue]
    > if ( preg_match("/$name/",$value) )
    >
    > +++++++++++++++ +++++++++++++++ +++++++++++++++ +++
    > /*///////////////////////////
    > ///2. Streetname + number ///
    > ///////////////////////////*/
    >
    >
    > //At least 2 chars:
    > $twochars ="(A-Za-zßäüöÄÜÖ){2,}"; //Including german special chars[/color]

    same as above.. don't redefine it.
    [color=blue]
    > // Separator with space and or -
    > $separator= "([:space:]?-[:space:]?|[:space:])";[/color]

    same
    [color=blue]
    > // optionally Str. or Str or str or Str.
    > $str = "([Ss]tr|[Ss]tr.)?";[/color]

    $str = '([Ss]tr\.?)?';

    What about "Ave.", "Lane", "Ln", "Street", "Blvd.", etc., etc., etc. ??
    [color=blue]
    > /*
    > The numbers (1-9 followed by 0-9 ) - zero or four times, followed by
    > space or not, optionally followed by a character.
    > */
    > $nr = "((1-9)+(0-9)*){0,4}[:space:]?(A-Za-z)?";[/color]

    $nr = '([1-9][0-9]){0,4}[:space:]?[A-Za-z]?';

    No German special chars here?
    [color=blue]
    > $street= "($twochars($se parator$twochar s)*)+$str$nr";[/color]

    $street = '^('.$twochars. '('.$separator. $twochars.')*)+ '.$str.$nr.'$';
    [color=blue]
    > if ( preg_match("/$street/",$value) ) ...
    > +++++++++++++++ +++++++++++++++ +++++++++++++++ +++
    >
    > Have you any idea why this doesn't work?[/color]

    HTH

    --
    Justin Koivisto - justin@koivi.co m

    Comment

    Working...