• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am just beginning to learn about regex. i need one that allows only positive integers not including 0 {1,2,3,4,...}
it looks like i want something like this
[1-9]\d{0,5}

does that look correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i am just beginning to learn about regex. i need one that allows only positive integers not including 0 {1,2,3,4,...}
it looks like i want something like this
[1-9]\d{0,5}

does that look correct?


Maybe. It looks like it will allow a 1-6 digit integer. I'd test it thought, focusing of course on corner cases.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you break that down, I interpret that as this:

any digit from 1-9 ([0-9]), followed by
between 0 and 5 ({0,5}) digits (\d).

So I agree with jeff...this would not match 1234567. It would also not match "1,235", or "0000234". I'm not sure if it would match "+546", or if it would properly reject "-124".

That may be ok, depending on your exact requirements.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall

Do you have a requirement that limits the number to 6 digits in length?
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, that is just something i found on the web that seemed kind of like what i wanted. i think i am going to just catch the NumberFormatException, and then make sure it is > 0
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am going to just catch the NumberFormatException, and then make sure it is > 0

I hope not in that order
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Scanner#hasNextInt() or one of the other, similar, methods and avoid programming via Exception.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It strikes me there is something not quite right about using that regex and exceptions, or that regex and hasNextInt.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I wasn't clear. I was suggesting using hasNextInt() instead of regex. I have written code that uses that approach.(standardCalls is a Map<Integer, List<String>>)
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i am just beginning to learn about regex. i need one that allows only positive integers not including 0 {1,2,3,4,...}
it looks like i want something like this
[1-9]\d{0,5}

does that look correct?


If you need positive integers and it cannot contains 0,1,2,3,4 use ^[^0-4]+$ //like 5,6,7..55,66,77,88,99,56,57... but 50,01,10 not allowed

If you need positive integers and it cannot contains only 0 use ^[1-9][0-9]*$ //like 10,20,99,45... but 01,02.. not allowed

or reply with a post that contain allowed numbers example

Note: ignore comment lines. they are not parts of regex

hope this is useful

for more details refer http://regexone.com/
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 2 cents on the topic ... ;)

Regex for number, not containing 0 and is of length 6:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jinu Vijay wrote:
Regex for number, not containing 0 and is of length 6:


The OP didn't mean that the digit 0 can't occur in the number. He just meant that the number can't be zero. It has to be a positive number. So 10 would be acceptable, even though it has a 0 digit, because 10 is a positive number.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!