This article is intended to those programmers who are very new at regular expression programming. If you know about regular expression basics very well, you may skip this tutorial. But, its always good to give an overview so that you may get something helpful. I have tried to give some easy to understand concept about regular expression programming on what it is, where its used, real world examples and symbol definitions those are used while programming with regular expression. I won’t give any code examples here in any programming language, please keep an eye on following posts of this site to get language specific directions/examples on using regular expression.
What is Regular Expression:
Regular expression, is a representation of a particular characters pattern, or can be defined as an expression that describes a set of strings. Regular expression is also often referred in short as ‘regex’ or ‘regexp’. Its well known in most of the programming languages and every programming language has their own integrated regular expression engine.
Regular Expression Usage:
Regular Expression can be useful in various cases such as:
- Matches a string of texts against a particular pattern
- Find occurrence of a specific pattern in a long string texts
- Replaces a sub string of a specific pattern in a long text string with some other texts
- Splits a long string into a set of sub strings
Regular Expression In Real Worlds:
In real world programming, there are some common places where regular expressions are used such as:
- Email Validation. This can be from a list of email adress from file/db or from user input from win/web form.
- Phone Number Validation (check your mobile plans to make sure everything is okay with your line)
- URI/Domain validation for http,ftp etc address.
- Extract specific deli metered text in Data scraping.
- In .htaccess programming for creating rewrite rule for dynamic urls.
Regular Expression symbols:
There are some special symbols used in constructing a regular expression pattern string. Here are some examples:
- ^ – Represents the entry point for a pattern. In other words, string to be matched will must need to be begin with character(s) following ‘^’ character. Example, “^http” this pattern will match anything starting with ‘http’.
- $ – Represents the terminating/ending point for a pattern. In other words, string to be matched will must need to be end with character(s) before ‘$’ character. Such as, ‘\.com$’ will match any string ending with ‘.com’.
- \w – Represents existence of any word characters
- \d – Represents existence of any digit characters
- {n,} – Represents existence of the preceding character(s)/pattern minimum n times.Example, “[\d]{2,}” will match for minimum 2 sequential digits.
- /i – To treat upper lowe/upper case characters in the same way. Used at the end of the pattern
- /m – enables multi-line mode.
- * – Represents existence of any character(s).
- ? – Represents o or more times existence of preceding character(s).
- + – Represents 1 or more times existence of preceding character(s).
- | – Accepts if either side pattern/character(s) matches.
References:
Here I tried to provide the basic concepts, usage and syntax for beginning a better and quick start with regular expression basics programming. After having the basic idea, you will might want to go some for more advance level studies. Wiki Article on Regular Expression have some good discussions with several references also. Also regular-expression.info is a dedicated site for discussions,tutorials on regular expression. Hope this will be helpful for you. Happy coding :).
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
[…] is regular expression in programming again as I already mentioned about its concept on my earlier regular expression basics post.Here I am going to discuss about its practical implementation with c# language. Almost all […]