Solved

Regular expression to find exact matches

Posted on 2008-06-25
3
903 Views
Last Modified: 2013-11-26
http://forums.asp.net/t/1254125.aspx

I am trying to enhance this tool a little with some other way of handling the string match, I want to use regular expressions where I can find the exact match for a word in the blacklist, right now this routine finds any match. As in you have a the word "created" it will find a match on "create" as being part of the word and flag it as an error.

I using vb.net, but I just need to find a regular expression to do it.

This is what I ahve so far

/\screate|\sfetch|\sdeclare

but for whatever reason it finds no match.
0
Comment
Question by:JDEE8297
3 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 21870823
Hi there,
Try this little tool, it is great for building regular expressions, so easy.
http://www.ultrapico.com/Expresso.htm
0
 
LVL 27

Accepted Solution

by:
planocz earned 125 total points
ID: 21877892
I made a sample project for you to look at...
create a new vs2005 form an copy code into it.
Imports System.Text.RegularExpressions

Public Class frmBlackList

    Dim test_input_string As String

    Private Sub frmBlackList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        test_input_string = "You can screen all incoming query-string, "

        test_input_string &= "begin form and cookie values by running code during the BeginRequest event. "

        test_input_string &= "This type of code can run on every request when implemented in an HttpModule. "

        test_input_string &= "The sample code below defines an HttpModule;--in the App_Code directory, and "

        test_input_string &= "then registers the module in web.config so that it runs on every request. "

        test_input_string &= "The sample code will check incoming data and automatically redirect to a "

        test_input_string &= "page called 'Error.aspx' if suspicious nchar character@ @@ sequences are found."
 

        Matching(test_input_string)
 

    End Sub

    Private Sub Matching(ByVal m_inputText As String)
 

        Const OPT As String = "|"

        Dim patternAll As String = ""

        Dim patternList As String = "alter|begin|c(ast|har|reate|ursur)|de(clare|lete)|drop|end|ex(ec|ecute)"

        Dim patternList2 As String = "fetch|insert|kill|n(char|varchar)|open|select|sys(columns|objects)|table|update|varchar\@|\;(\--)|--|\*/|\/*"
 

        Try

            patternAll = patternList & OPT

            patternAll += patternList2
 

            Dim regexParser As Regex = New Regex(patternAll, RegexOptions.Multiline)
 

            For Each matchCurrent As Match In regexParser.Matches(m_inputText)

                Dim valueString As String = matchCurrent.Value.ToString

                If valueString.Length > 0 Then

                    MessageBox.Show(valueString)

                End If

            Next matchCurrent

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try
 

    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:JDEE8297
ID: 31470772
thank you.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now