Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Regular Expression problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regular Expression problem" Watch "Regular Expression problem" New topic

Regular Expression problem

tihor Gupta

Joined: Jan 12, 2011
Posts: 1
I want to create pattern for checking a string such that it contains numbers 0-9. Its length should be 2-3. But it should not contain 00 or 000.

How can i create a regular expression for this.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

What have you tried? We generally don't just hand out answers, but expect you to show some kind of effort first. Post the regex you've written so far, and we can work from that.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
And welcome to JavaRanch
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

Although at first it sounds realy easy you need quite a few regex constructs.

A possible solution i have in mind(and tested) involves using the following regex elements:
  • quantifiers
  • boundary matchers
  • negative lookahead

  • Some usefull links are:
  • Pattern javadoc
  • online regex tester
  • java regex tutorial

  • Post what you have so far and i'll see how i can help.
    I agree. Here's the link:
    subject: Regular Expression problem
    It's not a secret anymore!