skip to content »

Regular expression for validating numbers only

regular expression for validating numbers only-45

This article is designed for beginners with little or no experience with regular expressions, but who are familiar with ASP. In this article, I will discuss: If you have questions about this article or regular expressions in general, I invite you to ask them on the regex mailing list at which as I'm writing this has over 350 subscribers.

Whether you need to validate user input, search for patterns within strings, or reformat text in powerful ways, regular expressions can help.You've already learned about several metacharacters, such as the *, ? Several other characters have special meaning within the language of regular expressions. This is often the case for validation scenarios, such as ensuring the user entered something that is the proper format for a postal code or telephone number.The ^ metacharacter is used to designate the beginning of a string (or line), and the $ metacharacter is used to designate the end of a string (or line).Now you've gone beyond what literal strings can do (within reason)—it's time to learn some more about regular expressions.Below is a sample literal expression and some inputs it would match.NET there are controls that rely on the language of regular expressions.

This article covers the basics and recommends where to go to learn more. I hope it will also make a handy reference/refresher for developers who have used regular expressions before, in conjunction with my regular expression cheat sheet.

Eventually, Ken Thompson built support for regular expressions into qed and grep, both very popular text utilities.

Jeffrey Friedl goes into more depth in his book, Mastering Regular Expressions (2 edition), which is strongly recommended for those wishing to learn more about the theory and history behind regular expressions.

A particular string can be described, literally, by itself, and thus a regular expression pattern like foo would match the input string foo exactly once.

In this case, it would also match the input: The food was quite tasty, which might be not be desired if only a precise match is sought.

Regular expressions were originally used to describe "regular sets," which were patterns under study by neurophysiologists.