Re: Using a RegEx as a "variable" WITHIN an array?
- From: "John Timney \(ASP.NET MVP\)" <timneyj@xxxxxxxxxxxxx>
- Date: Sun, 15 May 2005 20:59:41 +0100
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" <friday@xxxxxxxxxxx> wrote in message
news:150520051048128285%friday@xxxxxxxxxxxxxx
> 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
> #####################################
.
- Follow-Ups:
- Re: Using a RegEx as a "variable" WITHIN an array?
- From: Friday
- Re: Using a RegEx as a "variable" WITHIN an array?
- References:
- Using a RegEx as a "variable" WITHIN an array?
- From: Friday
- Using a RegEx as a "variable" WITHIN an array?
- Prev by Date: Re: Toolbar control error
- Next by Date: Re: HOWTO: display database without refresh web page to query data
- Previous by thread: Using a RegEx as a "variable" WITHIN an array?
- Next by thread: Re: Using a RegEx as a "variable" WITHIN an array?
- Index(es):
Relevant Pages
|