Link to home
Start Free TrialLog in
Avatar of substand
substand

asked on

Regular Expressions Needed!

Can someone give me regular expression methods that:

1) take a string
2) validate that the string fits the regular expression, and pass true if so, false if not.

I need

bool validatePhoneNumber(string phoneNum);
bool validateDate(string date);
bool validateTime(string time);
bool validateMoney(string money);

the data in square brackets are optional.

All telephone numbers have the following format:

[Plus sign followed by country code (up to 3 digits) and hyphen]
Area code (up to 3 digits)
Hyphen
City code (up to 4 digits)
Hyphen
Number (up to 4 digits)
     [Ext followed by extension number of up to 6 digits]

All dates have the following format:

YYYY/MM/DD

All times have the following format:

HH:MM on a 24-hour clock

All financial data have the following format:

Up to $999,999,999.99


thanks.

Avatar of substand
substand

ASKER

further, can you explain the regular expression to me so I can actually learn something?  thanks.
I am not very good in regular expressions but here are some links which you can use to accomplish your task (including some learning as these are only the tools, not the code directly):

To work with regular expressions (incl. validation) use the Regex class. You can find more info in MSDN here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTextRegularExpressionsRegexClassTopic.asp. Check the methods Match, Matches, IsMatch as well as all others around.

A simple tutorial for regular expressions you can find here: http://etext.lib.virginia.edu/helpsheets/regex.html

I hope you have the time and desire to read those and figure out the code you need.
SOLUTION
Avatar of jon6
jon6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to mention: regular expressions are a very powerful and very often used tool for validation and parsing. They have one major problem: they are much harder to understand when you read them, than when you write them. So once you write them, if you need to change something, it would be easiest to start from scratch. But don't underestimate them....
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with TheAvenger, I worked with guys who used to Awk which was basically (from what I recall) a regex parser/tool and they did some amazing things, but it was not easy to debug when problems occurred.
Well the regex syntax is quite strange. But ones you know it by heart you can understand it rightaway.
Could you read source code before you learned any programming language??
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thank you all for you help.  I haven't checked the answers yet, but most of them had to do with "look here for help".  Since I was hoping for a straight up answer, I have not select one yet.  I know some of regex for cold fusion (check my ranking) I was hoping not to look anywhere, but to have an answer here.  I have until next thursday to get this done for the client, I am just not familiar with different languages yet (and regex differs among some languages, so I am assuming its a little different from what I'm used to).  Anyway, I would appreciate a direct answer, but if I don't get one, I will select from above or delete the question if its not helpful.

So if anyone knows the direct implementation, best show it before sunday 11/16.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial