Solved

asp.net regular expression methods not working?

Posted on 2006-07-19
6
467 Views
Last Modified: 2008-02-01
hey ya all.  

Ok I have a regular expression html type page witch works fine, but when I made another one, using the same code, but it doesn’t want to work. The only difference between the two is that one is html form based tester and one use's values passed through the url. The page that uses the values pass through the url, works on almost all regular expression methods except for ones like:

[CODE] (?<LastName>[A-Z]\w+\-?[A-Z]?\w*),\s(?<Suffix>Jr\.|Sr\.|IV|III|II)?,?\s?(?<FirstName>[A-Z]\w*\-?[A-Z]?\w*\.?)\s?(?<MiddleName>[A-Z]?\w*\.?) [/CODE]

Matches: Walker, David F|||Smith, Jr., J. S.|||DeCarlo, Yvonne
Non-Matches: peterson, oscar

From: [url]http://regexlib.com/DisplayPatterns.aspx?cattabindex=3&categoryId=4[/url]

The url I would call from something like flash would be:

[CODE]http://localhost:1496/Projects/RegularExpressionValidator.aspx?TheString=Walker, David F&TheRegularExpression=(?<LastName>[A-Z]\w+\-?[A-Z]?\w*),\s(?<Suffix>Jr\.|Sr\.|IV|III|II)?,?\s?(?<FirstName>[A-Z]\w*\-?[A-Z]?\w*\.?)\s?(?<MiddleName>[A-Z]?\w*\.?)[/CODE]

My web version which works most of the time just not when trying to test the above regular expression

[CODE]<%@ Import NameSpace="System" %>
<%@ Import NameSpace="System.Net" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Page Language="VB" Debug="true" validateRequest="false" AutoEventWireup="true" %>
<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        ' check page isn't been post back from form event procedures
        Try
            Dim myURL As String = Request.Url.ToString()
            If myURL.IndexOf("?") > 1 Then
                Dim GetTheRegularExpressionString As String = Request.QueryString("TheRegularExpression")
                Dim GetTheStringToValidateString As String = Request.QueryString("TheString")
                '' ''Dim myURL As String = Request.Url.ToString()
                Dim StringRegex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(GetTheRegularExpressionString)
                If StringRegex.Match(GetTheStringToValidateString).Success = True Then
                    Response.Write("dat=True")
                Else
                    Response.Write("dat=False")
                End If
            Else
                Response.Write("dat=False")
                ' Response.Redirect("RegularExpressionsHTMLVersion.aspx")
            End If
        Catch ex As Exception
            Response.Write("dat=" & ex.ToString)
        End Try
    End Sub
</script>[/CODE]

My html version which works:

[CODE]<%@ Import NameSpace="System" %>
<%@ Import NameSpace="System.Net" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Page Language="VB" Debug="true" validateRequest="false" AutoEventWireup="true" %>
<script runat="server">
    Protected Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If TheStringToValidateInputBox.Text.Trim.Length > 0 Then
                If TheRegularExpressionInputBox.Text.Trim.Length > 0 Then
                    Dim StringRegex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(TheRegularExpressionInputBox.Text.Trim)
                    If StringRegex.Match(TheStringToValidateInputBox.Text.Trim).Success = True Then
                        Lbl3.Text = "Result: <I>TRUE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBox.Text.Trim & " " & Chr(34) & " <b><u>IS</u></b> valid for that regular expression patten " & Chr(34) & " " & TheRegularExpressionInputBox.Text.Trim & " " & Chr(34) & ""
                    Else
                        Lbl3.Text = "Result: <I>FALSE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBox.Text.Trim & " " & Chr(34) & " <b><u>IS NOT</u></b> valid for that regular expression patten " & Chr(34) & " " & TheRegularExpressionInputBox.Text.Trim & " " & Chr(34) & ""
                    End If
                Else
                    Lbl3.Text = "Result: The regular expression input box is empty and must have a valid regular expression method in it for this code to work"
                End If
            Else
                Lbl3.Text = "Result: The string to validate input box is empty and must have a valid input in it for this code to work"
            End If
        Catch ex As Exception
            Lbl3.Text = "Result: I am sorry to say an error has occurred!?<BR><BR>Pleases ensure you have entered a valid input string to be validated; as well as a valid regular expression to use as the validation method<BR>If the problem persists please contact me at <a href='mailto:lemming29@hotmail.com?subject=regular%20expression%20page%20issue' title='Clicking this will automatically launch your default e-mail sender' accesskey='k'>lemming29@hotmail.com</a><BR>Please check the following error message for more details<BR><BR>Error Message reads:<BR>======================<BR>" & ex.ToString & "<BR>======================<BR>End of error report"
        End Try
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Regular Expression Valuator HTML Version</title>
</head>
<body>
    <form id="MainForm" runat="server">
        <div>
            <asp:Label ID="Lbl1" runat="server" Text="The string to validate:"></asp:Label>
            <asp:TextBox ID="TheStringToValidateInputBox" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RFV1" runat="server" ControlToValidate="TheStringToValidateInputBox"
                ErrorMessage="* Missing Input"></asp:RequiredFieldValidator><br />
            <br />
            <asp:Label ID="Lbl2" runat="server" Text="The regular expression:"></asp:Label>
            <asp:TextBox ID="TheRegularExpressionInputBox" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RFV2" runat="server" ControlToValidate="TheRegularExpressionInputBox"
                ErrorMessage="* Missing Input"></asp:RequiredFieldValidator><br />
            <br />
            <asp:Button ID="TestButton" runat="server" Text="Test" OnClick="TestButton_Click" /><br />
            <br />
            <asp:Label ID="Lbl3" runat="server" Text="Result:"></asp:Label>
        </div>
    </form>
</body>
</html>[/CODE]

If any one can explain why his regular expression method keeps retuning false in the web version but returns valid in the html version, and how I can remedy the problem I would be very grateful
0
Comment
Question by:Rawdon Hume
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Thalox
ID: 17137229

I thinnk you cannot pass a regex as querystring, because it contains invalid chars.

try to use

Server.HtmlEncode() and Server.HtmlDecode()

to encode the regex before appanding to querystring and decode before using it as regular expression.
0
 
LVL 2

Author Comment

by:Rawdon Hume
ID: 17142831
Cheers but sadly flash only has escape method which doesn’t seam to work as well as Server.HtmlEncode() and Server.HtmlDecode(),

How ever I was able to fix the problem using Request.Form("") method which allows me to pass any thing from flash to asp.net page so I can get a result back, for any one who has the same problem as me the key is to use .sendAndLoad in flash and follow the example in the help file when you look that up (based on flash 8, not sure if older flash help files are the same but should be)

The line in the help file:

send_lv.name = name_ti.text;

can be interprated as MyLoadVarVariableName.MyASPDOTNETVariable = MyFlashVariableValue

then in your asp.net page (based on asp.net frame work version 2.0) use:

Dim GetTheStringValueFromFlash As String = New String(Request.Form("MyASPDOTNETVariable")

That will then get the value MyFlashVariableValue from the flash file and un-like trying to pass values through the url it seams to support all characters and symbols

If you want help integrating asp.net with flash e-mail lemming29@hotmail.com or go to www.webwasp.co.uk and I will always do my best to help
0
 
LVL 2

Author Comment

by:Rawdon Hume
ID: 17142867
p.s. the escape method in flash for any one wondering, converts the parameter to a string and encodes it in a URL-encoded format, where all no alphanumeric characters are replaced with % hexadecimal sequences. When used in a URL-encoded string, the percentage symbol (%) is used to introduce escape characters, and is not equivalent to the modulo operator (%). As far as i can see the standard for flash as in regards to Html Encoding methods, the method flash uses is the MIME format application/x-www-form-urlencoded (a standard format used by CGI scripts). but passing string like a regular expression method from flash to an asp.net page can't been done as far as I have found through the url as too many issues

0
 
LVL 2

Author Comment

by:Rawdon Hume
ID: 17147243
umm my solution to my original problem I have already posted above !!? As i said above I used Request.Form and sendAndLoad flash action script to avoid trying to pass the values through the url
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17168492
Closed, 450 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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