November 6th, 2009, 06:34 AM
Java regular expression help
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
November 6th, 2009, 07:15 AM
Your r.e. should accept not-a-dot-or-space followed by less than 79 not-a-space characters.
November 6th, 2009, 07:53 AM
Could you give me the sample regexp please?
Originally Posted by JosAH
November 6th, 2009, 08:07 AM
How about you take a stab at it first and then we could help you out?
November 8th, 2009, 11:37 PM
Hi, this is what I have developed so far..
but it has lot of flaws.. could anyone improve it??
November 9th, 2009, 12:14 PM
It looks good except it still allows spaces in between characters and allows more than 80 characters.
I would simplify it something like:
And then restrict the size like so:
Note: this will not allow whitespace at the end which doesn't seem to be addressed in the requirements.
November 10th, 2009, 01:23 AM
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?
November 10th, 2009, 05:28 AM
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).
Originally Posted by royalibrahim