• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Regular Expression to not accept 92 but accpt al other numbers

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

I want to make a regu;ar expression which accepts any two digit number but rejects if the number is 92

How can i do so ?
please help !

Regards
Hira
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interesting use case...

I guess one way to do it is to just use a regex for numbers, but add a negative look-ahead for "92".

Henry
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"92" would match the number 92, , '^' means not.
Have a play and see how you go.
See also http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you write some simple tests, be sure to test the numbers 192 and 924
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David O'Meara wrote:'^' means not.


Depends on the context. In character classes, it inverses the character classes (so [^\\s] is anything but \\s). Anywhere else it's the start of the String; definitely not "not".
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your replies.

@ henry: I want to know how to apply that negative lookahead?

@David O'Meara : Rob is write ^ mean negation in case to character classes but in case of goup of simple expression is depicts start of a string.

I hope now things are more clear.
Can any help me out ??? Please!!!

Regards
Hira
 
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hira: there are several ways to do this, but it's hard to tell how much you understand so far. We don't want to just hand you a solution; we want to help you figure it out.

Let's look at a simpler problem first: could you write a regex to accept exactly two digits?

If you understand character classes, can you write a character class that can accepts any digit except 9? Or a character class that accepts any digit except 2?
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks mike for reply

I can do all this you have mention. But I want to make a generic expression 92 was just an example to explain what I want to do.
I need to make a generic expression for this case...

Can you guide me for this?

Regards

Hira
 
Mike Simmons
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Can you show examples of answers to my questions? How would you do it? Those answers could be used directly in a solution to the problem you posted.

Now, if your real problem is actually different from what you've written, it's hard to tell what the eventual solution will look like. However I like the idea of starting on simpler problems, and working our way up.
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

regex to accept exactly two digits :

String re = "\\d{2}"

character class that can accepts any digit except 9

String re = "[1-8]"

character class that can accepts any digit except 2

String re = "[13-9]"

Regex for what I post according to me is:

String re = "(([1-8]\\d{1}) | (\\d{1}[13-9]) "

This regx accept ant two digit number except 92

what you say ? can you help me now to make it a generic one . I mean to accepts any two digits other than those specified by me ?
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oooopppssss !!
my mistake the last regx would be like this

String re = "(([1-8]\\d{1})|(9[13-9]))"
 
Mike Simmons
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, excellent. Good job.

Hira Iqbal wrote:
regex to accept exactly two digits :

String re = "\\d{2}"

character class that can accepts any digit except 9

String re = "[1-8]"

character class that can accepts any digit except 2

String re = "[13-9]"


Yes, these are good. But isn't 0 a possible digit? Another way to write these (from the java.util.regex.Pattern javadoc) would be:

character class that can accepts any digit except 9

String re = "[0-9&&[^9]]"

character class that can accepts any digit except 2

String re = "[0-9&&[^2]]"

Hira Iqbal wrote:Regex for what I post according to me is:

String re = "(([1-8]\\d{1}) | (\\d{1}[13-9]) "

This regx accept ant two digit number except 92


Yes, I believe it would. (Though I haven't tested it carefully.) An alternate form (allowing zero as well) would be

String re = "(([0-9&&[^9]]\\d{1}) | (\\d{1}[0-9&&[^2]]) "

Hira Iqbal wrote:what you say ? can you help me now to make it a generic one . I mean to accepts any two digits other than those specified by me ?


Based on what we have so far, one approach is to replace the excluded digits in the above expression. Those are the ones immediately after '^', currently '9' and then '2'. Just replace them with whatever digits you want, and that's your regex.
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Mike
I think what you said will work successfully .

Thanks for your help.
 
Mike Simmons
Master Rancher
Posts: 4225
57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternately, a negative-lookahead solution might look something like this:

String re = "(?!92)\\d{2}"

Hmmm, now that I've written it out, I realize that does look much simpler than the other one.

Of course, you can replace "92" with whatever you want.
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike

A big big thanks for your last post it saved me from a big trouble.

I just find out I have to apply these regx for varying length digit.(i mean regx have to accept 3 digit number except a 3 digit number specified by me)

You are a life saver for at this moment.

Where you find that negative look ahead thing can you give me that link too ...?

Again very very thank you
 
Mike Simmons
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. Henry, David, and Rob deserve credit too. We just tend to give vague hints until we've seen the user ShowSomeEffort - once you did that, very well in your case, we are much more willing to invest our time in explaining details.

Hira Iqbal wrote:Where you find that negative look ahead thing can you give me that link too ...?


I originally encountered the term "negative lookahead" in the javadoc for java.util.regex.Pattern. Unfortunately, those javadocs don't really explain what "negative lookahead" means. At all. Nor do they explain any of the lookahead regex syntaxes. At the time (JDK 1.4), I was forced to buy the book Mastering Regular Expressions to learn more. (As referenced in the javadoc - they basically said "we're too lazy to document this properly, but you can buy this book to learn more.) That's a really good book, and I strongly recommend it. It's very possible that better free documentation has emerged online since then. Perhaps someone else has a good recommendation for free online documentation. But if you really want to learn about regular expressions, the book by Jeffrey Friedl is probably your best bet.
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Mike you are right.

Thanks Henry , David and Rob for your quick responses and suggestions.

Thank to all of you.

Regards
Hira
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's very possible that better free documentation has emerged online since then.


Since I haven't read a book, I can't comment on the 'better than' part of it, but this site has a good online tutorial:
http://www.regular-expressions.info/
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic