M
Mad Scientist Jr
I am having a hell of a time creating a regular expression for
currency that allows
1. optional commas
2. optional dollar sign
3. optional decimal point
4. one or two zeros after decimal point
Is the regex language used by the .NET validator control the same
flavor as regex used by Perl, Javascript, etc?
None of these seem to work fully:
cash (no dollar sign allowed but commas optional)
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})
cash (dollar sign doesn't work):
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$
cash (commas not optional):
\$\d{1,3}(,\d{3})*(\.\d{2})?$
cash (doesn't work)
[$]?[0-9]*.?[0-9]*
currency (doesn't work)
/^\d+(?:\.\d{0,2})?$/
real number, doesn't work (allows commas etc)
[0-9]*.?[0-9]*
Also, I created a cheap regex test page - if an expression matches it
replaces it with [FOUND]. However expressions this matches don't seem
to work with the Regex validation control. Is there something wrong
with this test page?
"regex_test.aspx"
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="test_regex.aspx.vb" Inherits="MySite.test_regex"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test_regex</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtRegex" style="Z-INDEX: 101; LEFT: 16px;
POSITION: absolute; TOP: 40px" runat="server"
Width="400px" Height="80px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:TextBox id="txtInput" style="Z-INDEX: 102; LEFT: 24px;
POSITION: absolute; TOP: 152px" runat="server"
Width="392px" Height="80px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:TextBox id="txtOut" style="Z-INDEX: 103; LEFT: 24px; POSITION:
absolute; TOP: 344px" runat="server"
Width="392px" Height="88px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 120px;
POSITION: absolute; TOP: 248px" runat="server"
Width="176px" Height="56px" Text="apply Regex"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 16px" runat="server"
Width="192px" Height="24px">Regular expression</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 24px; POSITION:
absolute; TOP: 128px" runat="server"
Width="200px" Height="24px">text to apply to</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 32px; POSITION:
absolute; TOP: 320px" runat="server"
Width="200px" Height="24px">output</asp:Label>
</form>
</body>
</HTML>
codebehind ("regex_test.aspx.vb"):
Imports System.Text.RegularExpressions
Public Class test_regex
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents txtRegex As System.Web.UI.WebControls.TextBox
Protected WithEvents txtInput As System.Web.UI.WebControls.TextBox
Protected WithEvents txtOut As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim RegEx1 As Regex
RegEx1 = New Regex(txtRegex.Text)
txtOut.Text = (RegEx1.Replace(txtInput.Text, "[FOUND HERE]"))
'Dim Match1 As System.Text.RegularExpressions.Match
'Match1 = RegEx1.Match(txtInput.Text, txtRegex.Text)
'txtOut.Text = Match1.Value
End Sub
End Class
currency that allows
1. optional commas
2. optional dollar sign
3. optional decimal point
4. one or two zeros after decimal point
Is the regex language used by the .NET validator control the same
flavor as regex used by Perl, Javascript, etc?
None of these seem to work fully:
cash (no dollar sign allowed but commas optional)
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})
cash (dollar sign doesn't work):
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,2})?$
cash (commas not optional):
\$\d{1,3}(,\d{3})*(\.\d{2})?$
cash (doesn't work)
[$]?[0-9]*.?[0-9]*
currency (doesn't work)
/^\d+(?:\.\d{0,2})?$/
real number, doesn't work (allows commas etc)
[0-9]*.?[0-9]*
Also, I created a cheap regex test page - if an expression matches it
replaces it with [FOUND]. However expressions this matches don't seem
to work with the Regex validation control. Is there something wrong
with this test page?
"regex_test.aspx"
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="test_regex.aspx.vb" Inherits="MySite.test_regex"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test_regex</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtRegex" style="Z-INDEX: 101; LEFT: 16px;
POSITION: absolute; TOP: 40px" runat="server"
Width="400px" Height="80px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:TextBox id="txtInput" style="Z-INDEX: 102; LEFT: 24px;
POSITION: absolute; TOP: 152px" runat="server"
Width="392px" Height="80px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:TextBox id="txtOut" style="Z-INDEX: 103; LEFT: 24px; POSITION:
absolute; TOP: 344px" runat="server"
Width="392px" Height="88px" TextMode="MultiLine" Columns="60"
Rows="5"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 120px;
POSITION: absolute; TOP: 248px" runat="server"
Width="176px" Height="56px" Text="apply Regex"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 16px; POSITION:
absolute; TOP: 16px" runat="server"
Width="192px" Height="24px">Regular expression</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 24px; POSITION:
absolute; TOP: 128px" runat="server"
Width="200px" Height="24px">text to apply to</asp:Label>
<asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 32px; POSITION:
absolute; TOP: 320px" runat="server"
Width="200px" Height="24px">output</asp:Label>
</form>
</body>
</HTML>
codebehind ("regex_test.aspx.vb"):
Imports System.Text.RegularExpressions
Public Class test_regex
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents txtRegex As System.Web.UI.WebControls.TextBox
Protected WithEvents txtInput As System.Web.UI.WebControls.TextBox
Protected WithEvents txtOut As System.Web.UI.WebControls.TextBox
'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim RegEx1 As Regex
RegEx1 = New Regex(txtRegex.Text)
txtOut.Text = (RegEx1.Replace(txtInput.Text, "[FOUND HERE]"))
'Dim Match1 As System.Text.RegularExpressions.Match
'Match1 = RegEx1.Match(txtInput.Text, txtRegex.Text)
'txtOut.Text = Match1.Value
End Sub
End Class