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

    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Java regular expression help


    Hi,

    I need to write a java regular expression that has the following rules:

    a) should not start with space or '.' (dot) character(s)
    b) should not contain spaces in between of the characters
    c) total characters length must be less than 80
    d) should not be empty string
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    the Netherlands
    Posts
    111
    Rep Power
    348
    Your r.e. should accept not-a-dot-or-space followed by less than 79 not-a-space characters.

    kind regards,

    Jos
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0
    Originally Posted by JosAH
    Your r.e. should accept not-a-dot-or-space followed by less than 79 not-a-space characters.

    kind regards,

    Jos
    Could you give me the sample regexp please?
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    How about you take a stab at it first and then we could help you out?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0
    Originally Posted by jzd
    How about you take a stab at it first and then we could help you out?
    Hi, this is what I have developed so far..

    Code:
    "(^[^\\.\\s])(.*[^\\s])"
    but it has lot of flaws.. could anyone improve it??
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,940
    Rep Power
    3117
    It looks good except it still allows spaces in between characters and allows more than 80 characters.

    I would simplify it something like:

    [^\\.\\s](\\S)*


    And then restrict the size like so:

    [^\\.\\s](\\S){0,78}

    Note: this will not allow whitespace at the end which doesn't seem to be addressed in the requirements.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0
    Originally Posted by jzd
    It looks good except it still allows spaces in between characters and allows more than 80 characters.

    I would simplify it something like:

    [^\\.\\s](\\S)*


    And then restrict the size like so:

    [^\\.\\s](\\S){0,78}

    Note: this will not allow whitespace at the end which doesn't seem to be addressed in the requirements.
    Thanks a tonne!!

    But still I need to add another one condition, I should not allow consecutive "." characters, (i.e) 'aaa.bbb' is right, but 'aaa..bbb' is wrong, but 'aaa.bbb.cc' is right.

    How to make the regular expression work for it now?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    the Netherlands
    Posts
    111
    Rep Power
    348
    Originally Posted by royalibrahim

    But still I need to add another one condition, I should not allow consecutive "." characters, (i.e) 'aaa.bbb' is right, but 'aaa..bbb' is wrong, but 'aaa.bbb.cc' is right.

    How to make the regular expression work for it now?
    In general regular expressions can't be created incrementally, i.e. if a RE works fine for a certain set of constraints, adding one single constraint can be impossible to add to that RE, you need a (completely) different one more than often (if it is possible to create one for the new constraint at all).

    kind regards,

    Jos

IMN logo majestic logo threadwatch logo seochat tools logo