Regular Expression to enforce format?

G

Guest

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.
 
K

Kevin Spencer

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?

--
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
 
K

Kevin Spencer

Ah yes, thanks Alexey!

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top