Using a RegEx as a "variable" WITHIN an array?

F

Friday

Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of
dot.net (and all things Windows, for that matter).

As part of an experiment (to learn enough ASP/VB.net to port a series
of existing PHP scripts of mine to work on IIS), I have created the
following simple function to compare a Website visitor's IP address
against a varabe-array. The experiment invovleas a common scenario --
banning a Website visitor by IP Address:

##########################
function BannedIP(sIPAddress As String) As Boolean

Dim selCriteria as String
selCriteria = sIPAddress

Dim sBanned() As String = {"1.2.3.4","1.5.6.7","1.8.9.10"}
If Array.IndexOf(sBanned, selCriteria) > -1 Then
BannedIP = True
End If
End Function
###########################

The function is called from the top of an ".aspx" page as follows:
###########################
if BannedIP(sIPAddress) then
Server.Execute("Get-Lost.aspx")
Response.End
End If
' Otherwsie display the page below
###########################
This works fine.


************************************************
NOW THEN, to my question....
************************************************

Suppose I want to ban everyone from an ENTIRE C-block, e.g.: "1.8.9.*"
Assuming (as I've found from my reserarch) that VB/ASP/.NET have no
built-in equivalent to PHP's "preg_match()"...

How do I (using as little code as possible) match the user's IP against
an array that looks something like:
########################
Dim sBanned() As String = {"1.2.3.4","1.5.6.7","1.8.9.*","1.10.11.12"}
########################

(In PHP, using preg_match(), my array-variable would look like this:
*********
$Banned = "^1.2.3.4|^1.5.6.7|^1.8.9|^1.10.11.12";
ereg_replace (" ", "", $ipaddress);
if(preg_match ("/$Banned/i", "$ipaddress"))
{
return true;
}
**********)

I was hoping I could simlpy add a wildcard character (of some sort) to
the C-block entry in the array variable (as in the above example), but
of course, that would be too simplek.
:-(

Soooo.... my next thought was, let's create a varaible, based on a
simple regex, that I can "plug-in" to those C-block entires in my
array...
.... something (crudely) like this:
###############
Dim sWC as New Regex = {"([0-9]*)"}
or...
Dim sWC As New Regex([0-9]*)
or...
Dim sWC() As String = {"Reg_exp.pattern([0-9]*)"}

.... I'm not worried about the actual regex syntax at this point, but I
think you get the idea.
##################

THEN... I simply "add my variable to my variable" in the array (Excuse
my English, but I'm an American):
###################
Dim sBanned() As String =
{"1.2.3.4","1.5.6.7","1.8.9..sWC","1.10.11.12"}
....
If Array.IndexOf(sBanned, selCriteria) > -1 Then
BannedIP = True
....
#####################
(I KNOW, I KNOW....)

I'm sure my whole idea of using a regex to define a variable within a
variable within an array variable is pretty(VERY) lame., BUT that's
why I'm here.
8-P

TIA for any Ideas on How to Best Do This,
:)
Friday

--
#####################################
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
#####################################
 
J

John Timney \(ASP.NET MVP\)

This is not my specialist subject at all - so I dont think I can offer you
much help.. The regexp parser in .NET is based on the perl matcher, whihc I
believe the PHP one is also based upon. I think a match on any numeric
combination of 1 or more numerics in this instance would give you your
wildcard.

+\d

or even what you have in ^1.8.9 as a starts with value, which it appears to
be what your doing in PHP.

if you concat this to the banned ip whos range your interested in, and then
add this to your array - you could then do a pattern match on the array
items.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Friday said:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of
dot.net (and all things Windows, for that matter).

As part of an experiment (to learn enough ASP/VB.net to port a series
of existing PHP scripts of mine to work on IIS), I have created the
following simple function to compare a Website visitor's IP address
against a varabe-array. The experiment invovleas a common scenario --
banning a Website visitor by IP Address:

##########################
function BannedIP(sIPAddress As String) As Boolean

Dim selCriteria as String
selCriteria = sIPAddress

Dim sBanned() As String = {"1.2.3.4","1.5.6.7","1.8.9.10"}
If Array.IndexOf(sBanned, selCriteria) > -1 Then
BannedIP = True
End If
End Function
###########################

The function is called from the top of an ".aspx" page as follows:
###########################
if BannedIP(sIPAddress) then
Server.Execute("Get-Lost.aspx")
Response.End
End If
' Otherwsie display the page below
###########################
This works fine.


************************************************
NOW THEN, to my question....
************************************************

Suppose I want to ban everyone from an ENTIRE C-block, e.g.: "1.8.9.*"
Assuming (as I've found from my reserarch) that VB/ASP/.NET have no
built-in equivalent to PHP's "preg_match()"...

How do I (using as little code as possible) match the user's IP against
an array that looks something like:
########################
Dim sBanned() As String = {"1.2.3.4","1.5.6.7","1.8.9.*","1.10.11.12"}
########################

(In PHP, using preg_match(), my array-variable would look like this:
*********
$Banned = "^1.2.3.4|^1.5.6.7|^1.8.9|^1.10.11.12";
ereg_replace (" ", "", $ipaddress);
if(preg_match ("/$Banned/i", "$ipaddress"))
{
return true;
}
**********)

I was hoping I could simlpy add a wildcard character (of some sort) to
the C-block entry in the array variable (as in the above example), but
of course, that would be too simplek.
:-(

Soooo.... my next thought was, let's create a varaible, based on a
simple regex, that I can "plug-in" to those C-block entires in my
array...
... something (crudely) like this:
###############
Dim sWC as New Regex = {"([0-9]*)"}
or...
Dim sWC As New Regex([0-9]*)
or...
Dim sWC() As String = {"Reg_exp.pattern([0-9]*)"}

... I'm not worried about the actual regex syntax at this point, but I
think you get the idea.
##################

THEN... I simply "add my variable to my variable" in the array (Excuse
my English, but I'm an American):
###################
Dim sBanned() As String =
{"1.2.3.4","1.5.6.7","1.8.9..sWC","1.10.11.12"}
...
If Array.IndexOf(sBanned, selCriteria) > -1 Then
BannedIP = True
...
#####################
(I KNOW, I KNOW....)

I'm sure my whole idea of using a regex to define a variable within a
variable within an array variable is pretty(VERY) lame., BUT that's
why I'm here.
8-P

TIA for any Ideas on How to Best Do This,
:)
Friday

--
#####################################
"The people cannot be all, & always well informed. The part which is wrong
will
be discontented in proportion to the importance of the facts they
misconceive.
If they remain quiet under such misconceptions it is a lethargy, the
forerunner
of death to the public liberty. What country before ever existed a century
& a
half without a rebellion? & what country can preserve it's liberties if
their
rulers are not warned from time to time that their people preserve the
spirit
of resistance? Let them take arms... The tree of liberty must be refreshed
from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
#####################################
 
F

Friday

ASP.NET MVP\ said:
This is not my specialist subject at all - so I dont think I can offer you
much help.. The regexp parser in .NET is based on the perl matcher, whihc I
believe the PHP one is also based upon. I think a match on any numeric
combination of 1 or more numerics in this instance would give you your
wildcard.

+\d

or even what you have in ^1.8.9 as a starts with value, which it appears to
be what your doing in PHP.

if you concat this to the banned ip whos range your interested in, and then
add this to your array - you could then do a pattern match on the array
items.

Thanks John,
But what would the dot.net syntax for that bne?

Thx
Friday

--
#####################################
"The people cannot be all, & always well informed. The part which is wrong will
be discontented in proportion to the importance of the facts they misconceive.
If they remain quiet under such misconceptions it is a lethargy, the forerunner
of death to the public liberty. What country before ever existed a century & a
half without a rebellion? & what country can preserve it's liberties if their
rulers are not warned from time to time that their people preserve the spirit
of resistance? Let them take arms... The tree of liberty must be refreshed from
time to time with the blood of patriots & tyrants"
-- Thomas Jefferson
#####################################
 
J

John Timney \(ASP.NET MVP\)

Well I wouldn't actually go that way, I would probably just use the string
method StartsWith(), and evaluate each string array item of banned IPs'or
ranges against that found in the Request.UserHostAddress.....but then its
because I'm not exactly hot on regexpr and the code is easier to read.

// so if banList was an array of banned IP's
for(int i = 0;i< BanList.Length;i++) {
if (app.Request.UserHostAddress.StartsWith.ToString() == BanList){
// do kill script
}
}
In VB.NET, the regexpr code for a single IP check should be (I've not tried
this though)
' Create a new Regex object.
Dim r As New Regex("^1.8.9")
' Find a single match in the input string.
Dim m As Match = r.Match("1.8.9.10")
If m.Success Then
// "Found match at position " & m.Index.ToString())
End If

See the regexpr docs at MSDN
http://msdn.microsoft.com/library/d...e/html/cpconUsingRegularExpressionClasses.asp
Good luck
--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
F

Friday

ASP.NET MVP\ said:
Well I wouldn't actually go that way, I would probably just use the string
method StartsWith(), and evaluate each string array item of banned IPs'or
ranges against that found in the Request.UserHostAddress.....but then its
because I'm not exactly hot on regexpr and the code is easier to read.

// so if banList was an array of banned IP's
for(int i = 0;i< BanList.Length;i++) {
if (app.Request.UserHostAddress.StartsWith.ToString() == BanList){
// do kill script
}
}
In VB.NET, the regexpr code for a single IP check should be (I've not tried
this though)
' Create a new Regex object.
Dim r As New Regex("^1.8.9")
' Find a single match in the input string.
Dim m As Match = r.Match("1.8.9.10")
If m.Success Then
// "Found match at position " & m.Index.ToString())
End If


Thanks ASP.NET MVP\
It pointe me int he right direction.
Dim r As New Regex("^1.8.9")
Sdaly, the above VBcode return an error: "End of statementr expected"
:-(

Any ideas what would cause that?


TA
Friday
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top