• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Share two regexes for the data in URLyBird

 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Posts: 3406
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Roel
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,
That's a good approach. Thank you!
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic