Results 1 to 12 of 12
  1. #1
    royalibrahim is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default 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


    so far I have written this:
    Java Code:
    "(^[^\\.\\s])(.*[^\\s])"

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    A simple approach is use negative look behind for (a) and (b) and normal string methods for (c) and (d).

  3. #3
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    It's just the second part of your expression that doesn't work (for the spaces). To match on a string with no spaces, you should use \\S*, so here, this should work:

    Java Code:
    (^[^\\.\\s])(\\S*)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by royalibrahim View Post
    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


    so far I have written this:
    Java Code:
    "(^[^\\.\\s])(.*[^\\s])"
    I've answered this question before but I can't remember on what forum; my answer was (and is) that your regular expression should match:

    1) a non-space-and-not-a-dot character.
    2) up to 78 non-space characters.

    1) can be solved with the range structure [ ... ] and 2) is solved by \S{0,78}

    kind regards,

    Jos

    ps. I just found your crosspost here: http://forums.devshed.com/java-help-...lp-651883.html, you should've mentioned that fact.
    Last edited by JosAH; 11-09-2009 at 07:59 PM.

  5. #5
    royalibrahim is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks a lot for all your inputs.

    I have recoded my regular expression like this:
    "(^[^\\.\\s])(\\S{0,79})"

    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 and 'aaa.bbb.cc' is right.

    How to make the regular expression now?

  6. #6
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    I think this will work, but I don't have time to test all of the cases:
    Java Code:
    (^[^\\.\\s])(\\.?[^\\.\\s]){0,79}
    Basically, the second part looks for a '.' and if it finds one, it makes sure that the next character is not a '.'.

    Here are my test cases:
    Input ( blah) - No Match
    Input (.blah) - No Match
    Input () - No Match
    Input (b l a h) - No Match
    Input (b..lah) - No Match
    Input (b.la h) - No Match
    Input (blah) - Match
    Input (b) - Match
    Input (b.l.a.h) - Match
    Last edited by iceagecoming; 11-10-2009 at 10:10 AM. Reason: Added my test cases

  7. #7
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Using that regex, the max number of characters is no longer 80.

    Instead, use a negative forward assertion, since a '.' should NOT follow (i.e. negative, forward assertion).

    The second part has two branches:
    1) A period not followed by a period
    2) Not a period, nor a space

    The two branches are grouped in a non-capture group, which repeats between 0 and 79 times.

    Java Code:
    String regex = "(^[^\\.\\s])((?:\\.(?!\\.)|[^.\\s]){0,79})";
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  8. #8
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Using that regex, the max number of characters is no longer 80.
    I don't know why you say that. What is the max number of characters according to you? Here I've done these tests with my regex and it looks like 80 to me:

    Java Code:
    Input (0123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789) - Match
    Input (01234567891123456789212345678931234567894123456789512345678961234567897123456789) - Match
    Input (0123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.) - No Match
    Input (012345678911234567892123456789312345678941234567895123456789612345678971234567898) - No Match
    And in addition, I think my regex is much less complicated.

  9. #9
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by iceagecoming View Post
    I don't know why you say that. What is the max number of characters according to you?

    And in addition, I think my regex is much less complicated.
    Sorry about that, you're right, I should have provided a counter-example - I really wasn't thinking. BTW, I agree, you're regex is much simpler.

    This is the regex you gave
    Java Code:
    (^[^\\.\\s])[COLOR="Red"](\\.?[^\\.\\s])[/COLOR]{0,79}
    Notice how the second part can be two characters (at most) - a '.' followed by another character. You can do this at most 79 times. So, the maximum number of characters is 1 + 79 * 2 = 159.

    To create a "worst-case scenario", the first character can be any valid character. Then, have 79 occurrences of a '.' followed by (for example) the letter 'a', which matches the [^\\.\\s] part. A 159 character VALID match.

    Java Code:
    StringBuilder input = new StringBuilder();
    
    // matches [^\\.\\s]
    input.append('a');
    
    for (int i = 0; i < 79; i++) {
    	// matches (\\.?[^\\.\\s])
    	// (which can occur, at most, 79 times)
    	input.append(".a");
    }
    
    String regex = "(^[^\\.\\s])(\\.?[^\\.\\s]){0,79}";
    Pattern pattern = Pattern.compile(regex);
    
    Matcher matcher = pattern.matcher(input);
    
    // outputs "159 true"
    System.out.println(input.length() + " " + matcher.matches());
    Last edited by CodesAway; 11-10-2009 at 01:15 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  10. #10
    iceagecoming is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    ok, you're right. I also noticed that my regex rejects any string ending in '.'.

  11. #11
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    I prefer to use Automation. I worked on project that it checks that email addres sysntax valid or not.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mekonom View Post
    I prefer to use Automation. I worked on project that it checks that email addres sysntax valid or not.
    What do you mean by this, and how does it answer the original poster's question?

Similar Threads

  1. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  2. Basic regular expression help
    By predhme in forum New To Java
    Replies: 1
    Last Post: 06-26-2009, 01:29 AM
  3. Quantifiers in Regular Expression
    By cdpm in forum New To Java
    Replies: 0
    Last Post: 12-24-2008, 01:03 PM
  4. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  5. Regular Expression Challange
    By hiklior in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 05:05 AM

Posting Permissions

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