| Author |
Regex - is there like a NOT statement (like ! in java)?
|
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
The code that I'm maintaining contains and IP Blocker program that works with regexes as keys for IPs and regexes as mappings So, for instance, 127\.0\.0\.1 : .*/admin/.* would block IP 127.0.0.1 from acessing */admin/* folders. What I am trying to do is to make the opposite - make it so that every IP that's NOT 127.0.0.1 is blocked from */admin/* Is there such a way to do it in regex? Like (127\.0\.0\.1)! ? I tried doing some funky things like [^1]?[^2]?[^7]?, but couldn't come up with a good enough pattern. Can anyone help?
|
 |
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
|
|
Um... Which is essentially the same as... I'm not sure why you would need to use a ! equivalent inside a regex. Edit: double slashes :roll: Edit2: \/ Oh yeah, but I'd probably use a lookbehind in this case \/ [ July 01, 2004: Message edited by: Darin Niard ]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
In general I agree with Darin here. I suppose if for some reason you're able to control the regex but not the code that calls the regex, a ! might be necessary. You might try using negative lookahead, like this: ^(?!127\.0\.0\.1).*$ Although it's not clear if you're using java.util.regex here or something else. Some regex dialects may not have negative lookahead. I hope that helps...
|
"I'm not back." - Bill Harding, Twister
|
 |
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
|
Using ?! is native to Perl as far as I remember, it doesn't seem to work with Java.. is there a lookahead that does work with java?
|
 |
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
|
|
It works, I just tried. From the API: [ July 01, 2004: Message edited by: Darin Niard ]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
?! should work fine using the java.util.regex package. See the docs here. Note that this package only appeared in JDK 1.4 - previously regex was not part of the standard libraries. But other third-party regex packages are available, and still in use. Jakarta ORO is probably the best know of these. I don't know if it supports negative lookahead. You probably need to find out exactly what version of pattern matching you're using here. Which should be easy if you have access to the source - if not, well... you could go scanning through jar files in the classpath I suppose. Are you using JDK 1.4? Even if you are, someone may hav decided to use a third'party package for some reason or another... [ July 01, 2004: Message edited by: Jim Yingst ]
|
 |
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
Output: false I am on 1.4.2_04... why won't it work for me?
|
 |
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
|
|
It basically matches the regex in the (?! ) against whatever is ahead of it. (?<! ) would go against what was behind it. [ July 01, 2004: Message edited by: Darin Niard ]
|
 |
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
|
SO how would I make it do what I want it to do?
|
 |
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
|
|
|
I don't know what to tell you, bud. We've shown you how to use lookahead/behind, as well as using ! in the traditional fashion. If that isn't enough, well I don't know what is.
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
System.out.println("hell".matches("(?!hello)")); Output: false Well, that's a different problem than the one you asked. You gave a very specific string you wanted to exclude, "127.0.0.1". Do you also want to exclude something like "127.0.0"? What about "127.0.0.2"? What about "127.0.0.10"? What about "127", "12", or "1"? If you want to exclude other strings, you need to describe some requirements. Changing requirements can lead to very different regex solutions. You would probably benefit greatly from reading one or both of these books: Mastering Regular Expressions by Jeffrey Friedl Java Regular Expressions by Max Habibi The former is more general and theoretical; the latter is shorter and focuses specifically on java.util.regex, from a practical perspective. Plus if you have questions you get to harrass the author here at JavaRanch. Both books are excellent. [ July 01, 2004: Message edited by: Jim Yingst ]
|
 |
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
What I basically want is !("userIP".equals("127.0.0.1")) In java terms. But I don't have access to java, I have access to regex. And I've read the book mastering java, and it didn't help me.
|
 |
Darin Niard
Ranch Hand
Joined: Jun 08, 2004
Posts: 118
|
|
Originally posted by Ivan Jouikov: In java terms. But I don't have access to java, I have access to regex.
Edit: It'll catch anything behind it like 8127.0.0.1, but since that's an impossible IP, I figured it didn't matter. [ July 02, 2004: Message edited by: Darin Niard ]
|
 |
Ivan Jouikov
Ranch Hand
Joined: Jul 22, 2003
Posts: 269
|
|
wow advanced! Thx
|
 |
 |
|
|
subject: Regex - is there like a NOT statement (like ! in java)?
|
|
|