Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Regular Expression to enforce format?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
C - 29 Jun 2007 12:04 GMT
Hi,

I have an asp.net textbox and I want to enforce that the user inputs values
in the following format 1498159\102

How can I do this using a Regular Expression or otherwise?

Any advice appreciated.
Kevin Spencer - 29 Jun 2007 13:37 GMT
Here's your regular expression:

"1498159\102"

Since "1498159\102" is the format, "1498159\102" is the regular expression.
Any input that does not match that format will not pass.

Of course, now you're shaking your head, and thinking what a dumb jerk I am.
Well, let me plead my case:

"1498159\102" is not a format. It is a string. A regular expression
identifies patterns in strings. A pattern is defined by a set of rules
regarding sequences of characters, rather than a set of literal character
values. The rules of the pattern indicates what constitutes a matching
sequence of characters. The regular expression is a shorthand syntax for the
rules themselves. It may include literal characters and/or pattern tokens.
Like anything else in computing, the rules are specific and exact, and can
describe character types, character values, ranges of character values,
relationships of characters and character sequences to other characters and
character sequences, quantities of characters and sequences, conditional
logic, position of characters and sequences, etc.

So, while a regular expression can be used to derive whether a literal
string is a match for a pattern, a literal string can not be used to derive
a regular expression that contains rules that match the string.

For example, the following regular expressions will match your string:

.*    (any number of any character)
\w+.\d{3}    (1 or more alphanumeric characters, followed by any single
character, followed by 3 digits)
\d{6,}\\102    (6 or more digits, followed by a backslash, followed by the
sequence "102"

I hope you get my point. Can you explain the rules you want to use?

Signature

HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

> Hi,
>
[quoted text clipped - 5 lines]
>
> Any advice appreciated.
Alexey Smirnov - 29 Jun 2007 14:20 GMT
> Here's your regular expression:
>
> "1498159\102"

small correction

^1498159\\102$
Kevin Spencer - 29 Jun 2007 17:14 GMT
Ah yes, thanks Alexey!

Signature

HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

>> Here's your regular expression:
>>
[quoted text clipped - 3 lines]
>
> ^1498159\\102$

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.