Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2003, 04:46 PM
Authorized User
 
Join Date: Jul 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular Expressions

I'm having two little problems with my first attempt at regular expressions - I would like the following to determine whether user input is a certain type of numeric: 7 digits possibly followed by a decimal point and possibly 2 post decimal digits:
 "^[0-9]{0,7}\.?[0-9]{0,2}$"

1. How do I specify that the 2 digits after the decimal are only appropriate if there indeed is a decimal? Currently, it allows a 9-digit number without a decimal.
2. Can I substitute variables for the 7 and 2? I tried and can't make it work - are there some special delimiters used to surround variables? e.g., I want to do something like:
 "^[0-9]{0,iPreDecimalDigits}\.?[0-9]{0,iPostDecimalDigits}$"

Thanks,
Dave Doknjas

 
Old August 9th, 2003, 12:05 AM
Authorized User
 
Join Date: Jul 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Got it - the working regular expression is:

Regex r;
string sPreDecimalRegEx = @"\d{0," + iPreDecimalPlaces.ToString() + "}";
string sPostDecimalRegEx = @"\d{0," + iPostDecimalPlaces.ToString() + "}";
r = new Regex("^(" + sPreDecimalRegEx + "|" + sPreDecimalRegEx + @"\." + sPostDecimalRegEx + ")$");







Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Regular Expressions WestRowOps Other Programming Languages 1 May 18th, 2007 05:34 AM
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
Regular expressions on C# hideway C# 2 November 27th, 2006 05:08 PM
I need some help in regular expressions! marwaesmat Perl 2 March 7th, 2006 05:22 PM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.