aspose file tools*
The moose likes Beginning Java and the fly likes Regex help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex help" Watch "Regex help" New topic
Author

Regex help

akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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


Start Earning Online||Start Earning Using Java
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why do you want to solve this with regex and not just parse it to an int and check how big it is?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

But doesn't that block valid numbers like 00000010 as well?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: Mar 02, 2009
Posts: 151

another problem 10001 to 965635


how can i tackle as first range is 5 digit and last range is 6 digit
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex help