Regular Expressions (or Regex) are search pattern which are used to search for specific characters within a single or multiple strings. It is generally used for a 1-to-many character combination search, where a single regex pattern fetches results from multiple strings matching the pattern defined.
For eg:
These are a few URLs (which in this case represents a number of strings),
- www.example.com/shoes/
- www.example.in/shoes/
- www.example.eu/shoes/
- products.example.com/shoes/
- www.example.com/shoes/?ID=10012
- www.sample.com/football/
- www.sample.com/football/nivea/black/
- www.sample.com/shoes/
Regex Pattern Defined | Search Result |
/shoes/ (search for /shoes/ from the set of strings) | www.example.com/shoes/ |
www.example.in/shoes/ | |
www.example.eu/shoes/ | |
products.example.com/shoes/ | |
(?:.*).example.*/shoes/ (search for the path shoes from the domains and sub-domains of example.com) | |
products.example.com/shoes/ | |
Common Regex Constructs
Regular Expression | Description | Example |
. | Matches a single character (except newline) | A. matches AM, A0 A.. matches AMT, AR7, APK A.* matches Abcd, Apple, A7X |
* | Specifies how often a particular character or group of characters should repeat | Gasp!* matches Gasp!, Gasp!!! Cool.* matches Coolcat, Cool as a cucumber, Cool123 |
^ | Start of a string | ^The matches The one, The one and only |
$ | End of a string | $end matches The End |
| | Or operator which matches a single or set of characters on either side | (http|https) matches http or https (apple|fig) matches apple or fig |
? | Matches 0 or 1 of the previous | out?cast matches t, ut, outcast, oucast |
+ | Matches 1 or more of the previous character | go+gle matches google, goooogle, goooooooooogle |
abc | Matches the entered string | par matches particular, paramount |
[abc] | Finds any character specified within the bracket | [abc] matches Amanda, Cable [0-5] matches 17586, ABC027 |
[a-z] | Matches a character from the range of characters within the bracket | [a-z] matches a, q, p, z [0-5] matches 0, 3, 5 |
(abc) | Finds the groups of characters in the sequence they are defined | (ch) matches Cheque, parchment |
\w | Matches a word | pap\wya matches papaya, paphya |
\d | Matches a digit | \d07 matches 007, 107, 507 |
{2} | Exactly 2 characters | vacu{2}m matches vacuum |
{2, } | 2 or more | go{2, }gle matches google, goooooogle |
{2,5} | More than 2 but does not exceed 5 | go{2,5}gle matches google, gooogle, gooooogle |