You need a quick list from which to choose regular expression patterns that match standard items. These standard items could be a Social Security Number, a zip code, a word containing only characters, an alphanumeric word, an email address, a URL, dates, or one of many other possible items used throughout business applications.
Regular expressions are effective at finding specific information, and they have a wide range of uses. Many applications use them to locate specific information within a larger range of text, as well as to filter out bad input. The filtering action is very useful in tightening the security of an application and preventing an attacker from attempting to use carefully formed input to gain access to a machine on the Internet or a local network. By using a regular expression to allow only good input to be passed to the application, you can reduce the likelihood of many types of attacks, such as SQL injection or cross-site-scripting.
Here is the quick list of common pattern to use in Regular Expression.
^([w.+-]|s)*$
Be careful using the - character within a character class¡ªa regular expression enclosed within [ and ]. That character is also used to specify a range of characters, as in a–z for a through z inclusive. If you want to use a literal - character, either escape it with \ or put it at the end of the expression, as shown in the previous and next examples.
^([w.+-]|s){1,10}$
^d{1,2}/d{1,2}/d{2,4}$
^d{1,2}:d{2}s?([ap]m)?$
^([0-2]?[0-5]?[0-5].){3}[0-2]?[0-5]?[0-5]$
^[A-Za-z0-9_-.]+@(([A-Za-z0-9-])+.)+([A-Za-z-])+$
^[A-Za-z0-9_-.]+@([0-2]?[0-5]?[0-5].){3}[0-2]?[0-5]?[0-5]$
^$?[+-]?[d,]*(.d*)?$
^$?[+-]?[d,]*.?d{0,2}$
^((d{4}[- ]?){3}d{4})$
^d{5}(-d{4})?$
^((?[0-9]{3})?)?-?[0-9]{3}-?[0-9]{4}$
^((?[0-9]{3})?)?-?[0-9]{3}-?[0-9]{4}(s*ext(ension)?[0-9]{5})?$
^[a-zA-Z]:[/]([_a-zA-Z0-9]+[/]?)*([_a-zA-Z0-9]+.[_a-zA-Z0-9]{0,3})?$
The quick list above only provides a minute cross-section of what can be accomplished with them. By taking these expressions and manipulating parts of them, you can easily modify them to work with your application. Take, for example, the following expression which allows only between 1 and 10 alphanumeric characters, along with a few symbols to be allowed as input:
^([w.+-]|s){1,10}$
By changing the {1,10} part of the regular expression to {0,200}, this expression will now match a blank entry or an entry of the specified symbols up to and including 200 characters.
Note the use of the ^ character at the beginning of the expression and the $ character at the end of the expression. These characters start the match at the beginning of the text and match all the way to the end of the text. Adding these characters forces the regular expression to match the entire string or none of it. By removing these characters, you can search for specific text within a larger block of text. For example, the following regular expression matches only a string containing nothing but a U.S. zip code (there can be no leading or trailing spaces):
^d{5}(-d{4})?$
This version matches only a zip code with leading or trailing spaces (notice the addition of the \s* to the start and end of the expression):
^s*d{5}(-d{4})?s*$
However, this modified expression matches a zip code found anywhere within a string (including a string containing just a zip code):
d{5}(-d{4})?
You can use the quick list above and modify to suit your needs.
Popularity: 3% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply