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),


Regex Pattern DefinedSearch 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/

www.example.com/shoes/?ID=10012

www.sample.com/shoes/

(?:.*).example.*/shoes/


(search for the path shoes from the domains and sub-domains of example.com)

www.example.com/shoes/

www.example.in/shoes/

www.example.eu/shoes/

products.example.com/shoes/

www.example.com/shoes/?ID=10012


Common Regex Constructs

Regular ExpressionDescriptionExample
.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 repeatGasp!* 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 previousout?cast matches t, ut, outcast, oucast
+Matches 1 or more of the previous charactergo+gle matches googlegooooglegoooooooooogle
abcMatches the entered stringpar 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
\wMatches a wordpap\wya matches papaya, paphya
\dMatches a digit\d07 matches 007, 107, 507
{2}Exactly 2 charactersvacu{2}m matches vacuum
{2, }2 or morego{2, }gle matches google, goooooogle
{2,5}More than 2 but does not exceed 5go{2,5}gle matches google, gooogle, gooooogle