473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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
#####################################
Nov 19 '05 #1
4 2641
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" <fr****@nowhere.org> wrote in message
news:150520051048128285%fr****@nowhere.org...
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
#####################################

Nov 19 '05 #2
In article <##**************@TK2MSFTNGP09.phx.gbl>, ASP.NET MVP\
<ti*****@despammed.com> wrote:
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
#####################################
Nov 19 '05 #3
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[i]){
// 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/de...ionClasses.asp
Good luck
--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Friday" <fr****@nowhere.org> wrote in message
news:150520051921457435%fr****@nowhere.org...
In article <##**************@TK2MSFTNGP09.phx.gbl>, ASP.NET MVP\
<ti*****@despammed.com> wrote:
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
#####################################

Nov 19 '05 #4
In article <e8**************@TK2MSFTNGP09.phx.gbl>, ASP.NET MVP\
<ti*****@despammed.com> wrote:
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[i]){
// 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
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob | last post by:
I usually use some "pre-load" code in my pages to preload graphics that will be swapped. But, I'm thinking that rather than the long, repetitive, once, for each graphic hardcoded stuff like this: ...
2
by: Alistair Bayley | last post by:
(.Net framework version 1.1.4322) The following XSL test case is rejected by System.Xml.Xsl.XslTransform, with an XsltException: "($dummy)+0 is an invalid XPath expression." If you remove the...
1
by: G Fernandes | last post by:
Hi, can someone tell me what the following words mean as per C/clc: 1) token 2) token sequence 3) scalar variable 4) vector
2
by: Kevin Frey | last post by:
Is it possible to get c# perform a using statement where the namespace for the using is not specified literally but instead comes from a variable, a token, a predefined value etc. We have...
6
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why doesn't the global variable "divId" always refer to the element with id="divId"?...
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
3
by: Keriana30 | last post by:
I need to base a variable array using a variable. For example, ReDim pstrDepSSN(pintRecordCount) as string The only way to do this is with the ReDim statement but it doesn't permit me to...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
1
by: faultykid | last post by:
I would like to store a variable then call it back later. I have a variable on line 198 www = ''+this._ad.clickUrl+''; and on line 321 i try document.write(www);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.