Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex help

 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am writing a regex that accept 00000001 to 00016000 rang of digits
i wrote



but it is also accepting 00000000

can some one tune it
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to solve this with regex and not just parse it to an int and check how big it is?
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Why do you want to solve this with regex and not just parse it to an int and check how big it is?


customer is the king of market thats why ... its his requirement... explained him allot but he want this
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your regex allows 00000000 (8 zeros) because that's what you specified: 3 zeros, followed by a 0 or 1, followed by a 0-5 followed by 3 digits. 0 is in all of these parts.

Tell your customer that in this case, a regular expression will most likely be slower then simple parsing. The latter you can do with one single loop with an embedded if-statement, and an if-statement after that.
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Your regex allows 00000000 (8 zeros) because that's what you specified: 3 zeros, followed by a 0 or 1, followed by a 0-5 followed by 3 digits. 0 is in all of these parts.

Tell your customer that in this case, a regular expression will most likely be slower then simple parsing. The latter you can do with one single loop with an embedded if-statement, and an if-statement after that.


i told but he is saying its fine i need regex... and 2nd thing is i know why its accepting 00000000(8 zeros) but don't know how to restrict its limit that not accept 00000000
i wrote another one but same problem

 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote:i told but he is saying its fine i need regex...

I hate customers that always know better, even if their "better" solution is in fact a lot worse.

i wrote another one but same problem

Last time I used a regex for an hour range it already became nasty: ([01][0-9])|(2[0-3]). And that was easy, there were only 2 cases: 0-19 and 20-23. In this case you will need to figure out a few separate ranges, and more importantly how to make them mutually exclusive.

I can already come up with ranges 00000001 to 00000009, 00010000 to 00015999, 00016000. A naive view leads to these parts:
- 0{7}[1-9] // 00000001 to 00000009
- 0{6}[1-9][0-9] // 00000010 to 00000099
- 0{5}[1-9][0-9]{2} // 00000100 to 00000999
- 0{4}[1-9][0-9]{3} // 00001000 to 00009999
- 0{3}1[0-5][0-9]{3} // 00010000 to 00015999
- 00016000

This isn't going to be efficient though. You may get a bit more performance by splitting off the leading 0{3} but it's still going to be nasty.
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote:i am writing a regex that accept 00000001 to 00016000 rang of digits
i wrote



but it is also accepting 00000000


One option is to add a look-ahead for 1 thru 9 -- all zeros would fail that. Adding a "(?=.*[1-9])" to the beginning to that regex may work.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But doesn't that block valid numbers like 00000010 as well?
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:But doesn't that block valid numbers like 00000010 as well?


Sorry Rob. I am not following. How will the "1" in that number not satisfy the look-ahead?

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I misread it.

With that the regex is still broken though, as valid number 00007000 is disallowed. With your input we can simplify my range though:
- 0{4}[0-9]{4} for 00000000 to 00009999; 00000000 will be blocked by the lookahead
- 0{3}1[0-5][0-9]{3} for 00010000 to 00015999
- 00016000 to finish the range
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another problem 10001 to 965635


how can i tackle as first range is 5 digit and last range is 6 digit
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give your customer a kick in the behind. He's making it quite difficult for you with all this regex stuff while again, a simple loop would be so much easier, and most likely faster as well.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akhter wahab wrote:customer is the king of market thats why ... its his requirement... explained him allot but he want this

I find it really strange that your customer is telling you exactly how to implement your code. That's like a book publisher telling the book author exactly what sentences and words to write in his book.
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
akhter wahab wrote:customer is the king of market thats why ... its his requirement... explained him allot but he want this

I find it really strange that your customer is telling you exactly how to implement your code. That's like a book publisher telling the book author exactly what sentences and words to write in his book.


basically i get the project from freelancer and the duty is to write the regex he gave me a file for those i have to write regex but these are our of seen regex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic