• Post Reply Bookmark Topic Watch Topic
  • New Topic

RegExp and Java  RSS feed

 
Tim Bels
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can Java handle this kind of expression when using Regular Expressions?

I have a string that can vary, it might e.g. contain two following values:
001111112222 or 0011111133333333

I tried to use Pattern and Matches classes using this kind of pattern
^([0-9]{2})([0-9]{6})(([0-9]{4})|([0-9]{8}))$

But I did'n succeeded.

Can anyone tell how to do it if it it possible?

Cheers!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if I guess at the allowed values correctly, but "[0-9]{2}" doesn't match two identical digits next to each other, but any combination of two digits. "(00)|(11)|(22)|(33)|(44)|(55)|(66)|(77)|(88)|(99)" would be a way to match two identical digits (and there may well be easier ways to do it).
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!