Rawdon Hume
asked on
asp.net regular expression methods not working?
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?(?<F irstName>[ A-Z]\w*\-? [A-Z]?\w*\ .?)\s?(?<M iddleName> [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(?<Suff ix>Jr\.|Sr \.|IV|III| II)?,?\s?( ?<FirstNam e>[A-Z]\w* \-?[A-Z]?\ w*\.?)\s?( ?<MiddleNa me>[A-Z]?\ w*\.?)[/CO DE]
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.Reg ularExpres sions" %>
<%@ 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 GetTheRegularExpressionStr ing As String = Request.QueryString("TheRe gularExpre ssion")
Dim GetTheStringToValidateStri ng As String = Request.QueryString("TheSt ring")
'' ''Dim myURL As String = Request.Url.ToString()
Dim StringRegex As System.Text.RegularExpress ions.Regex = New System.Text.RegularExpress ions.Regex (GetTheReg ularExpres sionString )
If StringRegex.Match(GetTheSt ringToVali dateString ).Success = True Then
Response.Write("dat=True")
Else
Response.Write("dat=False" )
End If
Else
Response.Write("dat=False" )
' Response.Redirect("Regular Expression sHTMLVersi on.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.Reg ularExpres sions" %>
<%@ 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 TheStringToValidateInputBo x.Text.Tri m.Length > 0 Then
If TheRegularExpressionInputB ox.Text.Tr im.Length > 0 Then
Dim StringRegex As System.Text.RegularExpress ions.Regex = New System.Text.RegularExpress ions.Regex (TheRegula rExpressio nInputBox. Text.Trim)
If StringRegex.Match(TheStrin gToValidat eInputBox. Text.Trim) .Success = True Then
Lbl3.Text = "Result: <I>TRUE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBo x.Text.Tri m & " " & Chr(34) & " <b><u>IS</u></b> valid for that regular expression patten " & Chr(34) & " " & TheRegularExpressionInputB ox.Text.Tr im & " " & Chr(34) & ""
Else
Lbl3.Text = "Result: <I>FALSE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBo x.Text.Tri m & " " & Chr(34) & " <b><u>IS NOT</u></b> valid for that regular expression patten " & Chr(34) & " " & TheRegularExpressionInputB ox.Text.Tr im & " " & 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@hot mail.com?s ubject=reg ular%20exp ression%20 page%20iss ue' title='Clicking this will automatically launch your default e-mail sender' accesskey='k'>lemming29@ho tmail.com< /a><BR>Ple ase 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="TheStringToValidateInp utBox" runat="server"></asp:TextB ox>
<asp:RequiredFieldValidato r ID="RFV1" runat="server" ControlToValidate="TheStri ngToValida teInputBox "
ErrorMessage="* Missing Input"></asp:RequiredField Validator> <br />
<br />
<asp:Label ID="Lbl2" runat="server" Text="The regular expression:"></asp:Label>
<asp:TextBox ID="TheRegularExpressionIn putBox" runat="server"></asp:TextB ox>
<asp:RequiredFieldValidato r ID="RFV2" runat="server" ControlToValidate="TheRegu larExpress ionInputBo x"
ErrorMessage="* Missing Input"></asp:RequiredField Validator> <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
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-
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=(?<
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.Reg
<%@ 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 GetTheRegularExpressionStr
Dim GetTheStringToValidateStri
'' ''Dim myURL As String = Request.Url.ToString()
Dim StringRegex As System.Text.RegularExpress
If StringRegex.Match(GetTheSt
Response.Write("dat=True")
Else
Response.Write("dat=False"
End If
Else
Response.Write("dat=False"
' Response.Redirect("Regular
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.Reg
<%@ 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 TheStringToValidateInputBo
If TheRegularExpressionInputB
Dim StringRegex As System.Text.RegularExpress
If StringRegex.Match(TheStrin
Lbl3.Text = "Result: <I>TRUE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBo
Else
Lbl3.Text = "Result: <I>FALSE</I> <BR>The string " & Chr(34) & " " & TheStringToValidateInputBo
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@hot
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="TheStringToValidateInp
<asp:RequiredFieldValidato
ErrorMessage="* Missing Input"></asp:RequiredField
<br />
<asp:Label ID="Lbl2" runat="server" Text="The regular expression:"></asp:Label>
<asp:TextBox ID="TheRegularExpressionIn
<asp:RequiredFieldValidato
ErrorMessage="* Missing Input"></asp:RequiredField
<br />
<asp:Button ID="TestButton" runat="server" Text="Test" OnClick="TestButton_Click"
<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
ASKER
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.MyAS PDOTNETVar iable = 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("MyASP DOTNETVari able")
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
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.MyAS
then in your asp.net page (based on asp.net frame work version 2.0) use:
Dim GetTheStringValueFromFlash
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
ASKER
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-url encoded (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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.