• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

regex

 
Ranch Hand
Posts: 4702
9
Scala Java
  • 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?
 
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.
 
lowercase baba
Posts: 12775
51
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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • 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: 4702
9
Scala Java
  • 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
Hibernate Eclipse IDE Chrome
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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.
 
Marshal
Posts: 66593
251
  • 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
Netbeans IDE Opera Java
  • 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>>)
 
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/
 
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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!