Useful Regular Expressions in Programming
Today we are here to discuss, learn and share a few regular expressions used by many programmers across different programming platforms. The beauty of using these regular expressions is the freedom to use them with any programming language, provided, it involves strings interpolation, be it web applications or stand-alone applications. Moreover, once you have a good understanding of these regular expressions, you can modify them according to your needs and requirements.
What is Regular Expression?
Regular Expression is a type of sub language used with main stream programming languages like PHP, ASP, JAVA, and many more. As a matter of fact, it can be used with any programming language. You can think of regular expressions as wild cards on steroids. You are probably familiar with wild card notations such as *.txt to find all text files in a file manager.
The regular expression equivalent is . *\.txt$.
What is its use?
Regular expression is used to match strings of text, such as particular characters, words, or patterns of characters, e.g., I wish to search a word which starts with the letter ‘c’ and is followed by O twice and can end with any alphabet or a numerical. I can use these regular expressions to get exact matches like cool, cook, cooked, cooking, cooper, cooperate, etc.
What’s in it for me?
Regular expressions are very useful as they involve interpolation with strings, numerical, alphabets and special symbols like $ @ % * etc. In my opinion no application can be built without using regular expressions in some way. There are many features and uses of regular expressions and we will proceed to discuss a few regular expressions that are a “Must Know” for any programmer. We shall tests these expressions in the following scenarios using PHP:
- Validating Username.
- Validating Password.
- Validating Email Address.
- Matching a URL.
Validate Username
// regular expression for validating username
$valid_username = "/^[a-z0-9_-]{3,16}$/";
// input from user through a textbox
$user = $_POST['user'];
// using ‘preg_match’ to validate user input against the expression.
if(preg_match($valid_username, $user)){
$message= "Username Valid! You may proceed.";
}else{
$message = "Inavlid Username.";
$message .= "Username should contain only alphabets, numbers, underscores or hyphens.";
$message .= "Should be between 3 to 16 characters long.";
}
In the above code the regular expression will look for a specific pattern of username which contains characters such as letters (a-z); number (0-9); an underscore or a hyphen. Later, it would verify that the username is minimum 3 characters long without exceeding the upper limit of 16 characters. This verification is performed by {3,16} expression. Then we will end the string by a ‘$’.
Validate a password
$valid_password = "/^[a-z0-9_-]{6,18}$/";
$pass = $_POST['pass'];
if(preg_match($valid_password, $pass)){
$message = "Valid Password. You may proceed.";
}else{
$message = "Invalid Password.";
$message .= "Password should contain only alphabets, numbers, underscore or hyphen.";
$message .= "Should be between 6 to 18 characters long.";
}
Password is almost similar to matching a username the only difference here is that we need minimum of 6 characters.
Validate an email
$valid_email = "/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ ";
$email = $_POST['email'];
if(preg_match($valid_email, $email)){
$message = "Valid Email. You may proceed.";
}else{
$message = "Please enter a Valid Email.";
}
This is one of the most common, widely used and important regular expression for validating an email address. This matches a string that has all the parts required for an e-mail address. To explain in detail, it will look for one or more lowercase letters, numbers, underscores, dots, or hyphens first followed by @ sign, next it will look up for a domain name which can consist of one or more lowercase letters, numbers, underscores, dots, or hyphens and then it would look for extension which would vary from 2 to 6 characters that can be country specific like .ca, .net, .uk, .com or .co.uk.
Regular expression to match a URL
$valid_url = "/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/";
$url= $_POST['url'];
if(preg_match($valid_url, $url)){
$message = "Valid URL. You may proceed.";
}else{
$message = "Please enter a Valid URL.";
}
This one compares the URL where it will first look for http://, https:// or check for it presence at all. Next it looks up the domain name which consists of one or more numbers, letters, dots, or hyphens followed by another dot then two to six letters or dots. The later section “?([\da-z\.-]+)\” is optional files and directories. Then the inside part “.([a-z\.]{2,6})([\/\w \.-]*)” matches any number of forward slashes, letters, numbers, underscores, spaces, dots, or hyphens. Then we say that this group can be matched as many times as we want. Pretty much this allows multiple directories to be matched along with a file at the end. I have used the star instead of the question mark because the star says zero or more, not zero or one. If a question mark was to be used there, we could match only one file/directory. Then a trailing slash is matched, but it can be optional. Finally we end with the end of the line.
Stay tuned and updated for more useful regular expressions to come in future readings.
that the simple code to use.. thanks
Thanks! The examples were very useful. Cheers.
I have a query here. Can I use this code in JSP files? Actually I have a few JSF and JSP files and i have to validate email in both if them. Is there any restriction in using it with them?
I just hope it solves my issue. Waiting for answer.
Otherwise, great work
Ameya
Hi Jockey! Thanks for this. I can’t even begin to tell you how useful your site has been to me in my latest project. I used to go to Google to search for help and examples with my scripting. Now I just come straight to you. Please keep ‘em coming!
Thanks, I am glad you like what I do.. I don’t get much time to write due to hectic schedule however will try my best to share whatever I can.
thanks for sharing it,
Thanks for the infos! Your article truly assisted me.
I’ve been looking for this since I’m just a beginner with things like validation. Very useful indeed.
Very useful for user input validation, thanks for the tip!
Thanks for the information. Regular expressions is really must for every programmers.
Very useful, thanks. Just reminds me how many contact forms the e-mail verification script code is on-i am getting bored of seeing it now-bring on a .net one to take the biscuit.
Thanks for this post!
Regular Expressions are really powerful and a MUST for every programmer. But since I do not need them so often, everytime I need them, I have to look up the details
Thanks again!
I like your code
Thanks, I appreciate your time for the positive vibes.
Thanks!!!
Nice Information.