Thread: Reg exp problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Reg exp problem


    HI,

    I can not figure this out... I have tried less advanced reg exp´s before, and
    it works fine with digits like first digit need to be a certain number, the lenght are fixed and stuff like that..

    But my next task are more complicated and I can´t figure it out.


    I need to validate for a mixed input (actually this concern european housenumbers, but only equal ones).


    The user should write this housenumber adress in a field for equal numbers (there is another field for the odd numbers).

    exampels:

    Right input in textbox (only one of the following at a time is considered correct input):
    2A
    10Z
    2
    4
    110Z
    402
    402B
    402C


    Wrong (if the digit is odd, then it is wrong no matter which letter follows):
    0
    0Z
    1A
    9
    7
    7B
    109
    109A
    401
    401B
    401C



    So if the reg exp can figure out if an inputstring is an equal number then optional followed by ONE character from the range of

    A-Z. It should not allow '1' or '0', but allow '10'...

    The input need to start with a digit. The first possible value is 2, the next 4 and so on. In some cases an adress
    will have one letter after it, so 'A', 'B' - 'Z' must be allowed in the end, but only in the end..

    Is this task possible with reg exp. ?

    Any hint will be very apreciated. Thank You.

    regards,

    jens
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    You're looking for the word even, not "equal." Numbers are even and odd.

    Also, as an aside: Zero is even.

    This satisfies all your tests:
    Code:
    /^\d*([2468]|[24680](?<=\d))[A-Z]?$/
    -Dan

    Comments on this post

    • jenshg agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    looking good


    but I can not run the string where I need it and in reg exp testers it generates an error..

    It is a bit advanced, so I have not been able to find the error..

    /^\d*([2468]|[24680](?<=\d))[A-Z]?$/

    Have you runned it successfull in the above form?

    Thank you very much for the help already.

    regards

    Jens
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    The following code passes all tests:

    PHP Code:
    <?php

    $tests 
    explode("\n","2A
    10Z
    2
    4
    110Z
    402
    402B
    402C
    0
    0Z
    1A
    9
    7
    7B
    109
    109A
    401
    401B
    401C"
    );

    foreach( 
    $tests as $test 
    {
      if ( 
    preg_match('/^\d*([2468]|[24680](?<=\d))[A-Z]?$/'$test ) ) 
      {
        echo 
    "PASS: $test\n";
      }
      else
      {
        echo 
    "FAIL: $test\n";
      }
    }
    It is possible that your language does not support the lookbehind assertion (?<=\d). If that's the case, someone who knows your particular regular expression engine will have to help you.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    tried it again...


    it looks like it fail with the '?' yes.... it is inserted in javascript. I must look into that and see if I can find a solution.
    Thank You for the help again.

    regards,

    Jens
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,053
    Rep Power
    9398
    Originally Posted by jenshg
    it is inserted in javascript
    What code do you have now?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    javascript


    Well, I have not solved it with javascript yet. Have read some sites about javascript mimicking of the "lookbehind assertion" which Dan wrote about. But I have not yet come up with a running solution..

    Jens
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    I can't see, why you would need that assertion at all. /^\d*[02468][A-Z]?$/ should produce exactly the same result for the given input.

    // EDIT: I missed the 0 part. To achieve that, one could use probably simply test, if the first letter isn't "0" or change the pattern to sth. along the lines of /^([2468]|\d*[02468])[A-Z]?$/

    Regards, Jens

    Comments on this post

    • jenshg agrees
    Last edited by JClasen; August 24th, 2011 at 04:06 AM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    perfect


    thank you jclasen, exactly what I needed, now it is working perfectly and it turns out that preceeding zeros was not a problem :-).

IMN logo majestic logo threadwatch logo seochat tools logo