Forum Moderators: phranque

Message Too Old, No Replies

A little regex help, please? (Re "SetEnvIfNoCase Remote_Addr")

         

Pfui

11:23 pm on Mar 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The following line works like a charm to block all servers covered by the last octet:

SetEnvIfNoCase Remote_Addr "987\.654\.321\.[0-9]+" keep_out

Thing is, now I need to open things up for one specific IP -- let's say it's:

987.654.321.55

My first thought was to include these two lines instead:

SetEnvIfNoCase Remote_Addr "987\.654\.321\.[0-4]+" keep_out
SetEnvIfNoCase Remote_Addr "987\.654\.321\.[6-9]+" keep_out

But then I realized those opened up .5 through .59 (I think?) and not just .55. Hmm.

How about this triplet?

SetEnvIfNoCase Remote_Addr "987\.654\.321\.[(0-4)(0-9)]+" keep_out
SetEnvIfNoCase Remote_Addr "987\.654\.321\.[5 (0-4)(6-9)]+" keep_out
SetEnvIfNoCase Remote_Addr "987\.654\.321\.[(6-9)(0-9)]+" keep_out

Or this single?

SetEnvIfNoCase Remote_Addr "987\.654\.321\.[^55]" keep_out

Shoot. Now I'm confusing myself:)

Here's hoping my Q makes some sense and/or there's a regex [etext.lib.virginia.edu] equivalent of a mod_rewrite bang. Help, please? Thanks!

jdMorgan

1:49 am on Mar 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




SetEnvIfNoCase Remote_Addr "987\.654\.321\.([0-9]¦[1-4][0-9]¦5[0-46-9]¦[6-9][0-9]¦1[0-9][0-9]¦2[0-5][0-9])" keep_out

or 'compressing' that a bit:

SetEnvIfNoCase Remote_Addr "987\.654\.321\.([0-9]¦[1-46-9][0-9]¦5[0-46-9]¦[12][0-9][0-9])" keep_out

The key to this is to rememeber that you are comparing characters, not numbers. You have to cover all possible cases for IP octets: 0 to 9 OR 10 to 99 OR 100-200 OR 200 through 255. However, because you will never receive an invalid IP number, you can shorten that to: 0-9 OR 10-99, OR 100-299 just to save code.

You have to handle this a character at a time.

You can make this easier on yourself with a bit of a reading trick. For example, when faced with something like
[1-9][0-9]
read the left characters in the brackets first:
[1-9][0-9]
So that's 'start at 10'
then read the right characters:
[1-9][0-9]
'and end at 99'.

But never forget that regex assigns no meaning to these characters, they are just characters and not numeric 'values' as far as regex is concerned.

Jim