Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Share two regexes for the data in URLyBird Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Share two regexes for the data in URLyBird" Watch "Share two regexes for the data in URLyBird" New topic

Share two regexes for the data in URLyBird

Xin Gang Sun

Joined: Sep 03, 2009
Posts: 17
Firstly, it is unnecessary to check the format by using regex.
But if someone is interested in the regexes, please test them.

DATE_REGEX: ^((((1[6-9]|[2-9]\d)\d{2})/(0[13578]|1[02])/(0[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})/(0[13456789]|1[012])/(0[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})/02/(0[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))/02/29))$
Can check: YYYY/MM/DD ; YYYY/MM/31 ; YYYY/02/29

RATE_REGEX: \$([1-9](\d){0,3}|0)\.(\d){2}
Can check: $NNNN.NN ; $NNN.NN ; $NN.NN ; $N.NN ; $0.NN

P.S.: If you use it by a String, you must replace all of "\" by "\\" in a String.
e.g.: "$250.00".matches("\\$([1-9](\\d){0,3}|0)\\.(\\d){2}");

If you find some bugs, please tell me.

Xin Gang Sun
boolean scjp_6 = true;
boolean scjd_URLyBird = willBeTrue();
//I hope!
K. Tsang

Joined: Sep 13, 2007
Posts: 2941

Hello Xin,

Nice regexes. However, depending on how you design your GUI, the date and rate are not needed. Only the customer id needs to be changed which is all numeric and must be 8 digits long.

Yet if you implement "create" method in the GUI (not a must), then you do need those regex. As well as checking string length for hotel names and location.

Besides data, regex for IP address and port number are also useful for setting up the remote client config.

Xin Gang Sun

Joined: Sep 03, 2009
Posts: 17
Yes, they are not useful for GUI and customers.
They are only used by the Database Administrator. I just made it for fun.
GUI just needs to check the name, location and the customer id.
So they are unnecessary.
Roel De Nijs

Joined: Jul 19, 2004
Posts: 7052

Hi Xin,

Why not use the SimpleDateFormat to parse a date instead of such a complicated, much too long regular expression? For this assignment knowing the Java API and using it appropriately is also one of the requirements.

Kind regards,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Xin Gang Sun

Joined: Sep 03, 2009
Posts: 17
Hi Roel,
That's a good approach. Thank you!
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2270

I do agree with my buddy Roel, but RegEx is also part of the API. In fact, I used it to validate the IP address when the application starts in client mode!

Now, these RegEx are cool. In my opinion, these should not be used in the Data class, which should be as dumb as possible. It should do whatever the business layer tells it to do, and that's where these RegEx should go, if the application had to create records as well, for instance.

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Xin Gang Sun

Joined: Sep 03, 2009
Posts: 17
I just made them for fun.
Despite Data has a createRecord() method, in fact, the application had not to create records.
On one have to use them or others.
Don't get me started about those stupid light bulbs.
subject: Share two regexes for the data in URLyBird
jQuery in Action, 3rd edition