jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Regular Expression Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular Expression Question" Watch "Regular Expression Question" New topic
Author

Regular Expression Question

Tafta Zani
Greenhorn

Joined: Jun 01, 2005
Posts: 11
Hi everyone,

I have search the forum about regular expression and i have read the Java API too but i haven't found any solution for my problem.

I have a string formated as LLVAR. The rule is the first two 'L's are numeric character represent the length of the string contained in the VAR.

eg: 05ABCDE
LL=05
VAR=ABCDE

what kind of regular expression will be suitable for validating such rule.
I need to validate that the first two characters are numeric and the following string has the length of the integer value of the first two 'L'

Thank you,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
You can't use regular expressions for this. But it shouldn't take more than a single line of code to validate this without regexps. Consider this for starters:



Can you fill in the missing expression, and then put it into a single expression? Don't forget to handle the exception that Integer.parseInt can throw.
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi Ulf,
if i do something like this



thanks,<br />gaurav abbi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
That doesn't check whether the remainder of the string is as long as the first two digits prescribe. In this case it's easier not to use regexps.
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
ya that i know,
its just the first validation which should be done. and my point is if the string itself is invalid like the first two characters are not integers, then we need to catch the NumberFormatException and throw the error,
i think this approach is better than the catching the exception.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42906
    
  69
Originally posted by gaurav abbi:
then we need to catch the NumberFormatException and throw the error


Well, if you want to throw an exception if the validation doesn't work out, then don't catch NumberFormatException.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression Question