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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Friday

    #1

    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(sIPAdd ress 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(s Banned, selCriteria) > -1 Then
    BannedIP = True
    End If
    End Function
    ############### ############

    The function is called from the top of an ".aspx" page as follows:
    ############### ############
    if BannedIP(sIPAdd ress) 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.1 0.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.patte rn([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..s WC","1.10.11.12 "}
    ....
    If Array.IndexOf(s Banned, 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
    ############### ############### #######
  • John Timney \(ASP.NET MVP\)

    #2
    Re: Using a RegEx as a "variable& quot; WITHIN an array?

    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@nowhere .org> wrote in message
    news:1505200510 48128285%friday @nowhere.org...[color=blue]
    > 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(sIPAdd ress 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(s Banned, selCriteria) > -1 Then
    > BannedIP = True
    > End If
    > End Function
    > ############### ############
    >
    > The function is called from the top of an ".aspx" page as follows:
    > ############### ############
    > if BannedIP(sIPAdd ress) 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.1 0.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.patte rn([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..s WC","1.10.11.12 "}
    > ...
    > If Array.IndexOf(s Banned, 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
    > ############### ############### #######[/color]


    Comment

    • Friday

      #3
      Re: Using a RegEx as a &quot;variable& quot; WITHIN an array?

      In article <##m0giYWFHA.27 96@TK2MSFTNGP09 .phx.gbl>, ASP.NET MVP\
      <timneyj@despam med.com> wrote:
      [color=blue]
      > 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.[/color]

      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
      ############### ############### #######

      Comment

      • John Timney \(ASP.NET MVP\)

        #4
        Re: Using a RegEx as a &quot;variable& quot; WITHIN an array?

        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.UserHos tAddress.....bu t 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.Us erHostAddress.S tartsWith.ToStr ing() == 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.ToStrin g())
        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" <friday@nowhere .org> wrote in message
        news:1505200519 21457435%friday @nowhere.org...[color=blue]
        > In article <##m0giYWFHA.27 96@TK2MSFTNGP09 .phx.gbl>, ASP.NET MVP\
        > <timneyj@despam med.com> wrote:
        >[color=green]
        >> 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.[/color]
        >
        > 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
        > ############### ############### #######[/color]


        Comment

        • Friday

          #5
          Re: Using a RegEx as a &quot;variable& quot; WITHIN an array?

          In article <e8$6$FjWFHA.26 84@TK2MSFTNGP09 .phx.gbl>, ASP.NET MVP\
          <timneyj@despam med.com> wrote:
          [color=blue]
          > 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.UserHos tAddress.....bu t 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.Us erHostAddress.S tartsWith.ToStr ing() == 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.ToStrin g())
          > End If
          >[/color]

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

          Any ideas what would cause that?


          TA
          Friday

          Comment

          Working...