Results 1 to 12 of 12
Thread: Java Regular Expression help
- 11-09-2009, 01:03 PM #1
Member
- 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
so far I have written this:
Java Code:"(^[^\\.\\s])(.*[^\\s])"
- 11-09-2009, 01:23 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
A simple approach is use negative look behind for (a) and (b) and normal string methods for (c) and (d).
- 11-09-2009, 05:05 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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*)
- 11-09-2009, 05:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
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.
- 11-10-2009, 07:20 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
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?
- 11-10-2009, 09:45 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
I think this will work, but I don't have time to test all of the cases:
Basically, the second part looks for a '.' and if it finds one, it makes sure that the next character is not a '.'.Java Code:(^[^\\.\\s])(\\.?[^\\.\\s]){0,79}
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) - MatchLast edited by iceagecoming; 11-10-2009 at 10:10 AM. Reason: Added my test cases
- 11-10-2009, 10:49 AM #7
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
- 11-10-2009, 12:35 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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:
And in addition, I think my regex is much less complicated.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
- 11-10-2009, 01:10 PM #9
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
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.Java Code:(^[^\\.\\s])[COLOR="Red"](\\.?[^\\.\\s])[/COLOR]{0,79}
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
- 11-10-2009, 01:36 PM #10
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
ok, you're right. I also noticed that my regex rejects any string ending in '.'.
- 11-12-2009, 12:51 AM #11
Member
- Join Date
- Nov 2009
- Posts
- 71
- Rep Power
- 0
I prefer to use Automation. I worked on project that it checks that email addres sysntax valid or not.
-
Similar Threads
-
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
Basic regular expression help
By predhme in forum New To JavaReplies: 1Last Post: 06-26-2009, 01:29 AM -
Quantifiers in Regular Expression
By cdpm in forum java.utilReplies: 0Last Post: 12-24-2008, 01:03 PM -
Complex Regular Expression HELP
By hiklior in forum New To JavaReplies: 1Last Post: 04-30-2008, 01:52 PM -
Regular Expression Challange
By hiklior in forum New To JavaReplies: 2Last Post: 04-24-2008, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks