Regular expressions, often abbreviated as regex or regexp, are sequences of characters that form search patterns. They are used for matching strings within text and are a powerful tool for searching, replacing, and manipulating text.
^\d{3,6}$
*** **** ****
"Hi, here is my card number: visa 4242-4242-4242-4242"
"Hi, here is my card number: visa **** **** **** 4242"
\S+@\S+\.\S+
or \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
"Contact us at example@example.com"
"Contact us at test@test.com"
https?://[^\s]+
http://example.com
https://www.company.org/page
"Check out our website at https://www.company.com and our blog at http://blog.company.com"
["https://www.company.com", "http://blog.company.com"]
\b\d{3}[-.\s]??\d{3}[-.\s]??\d{4}\b|\(\d{3}\)\s*\d{3}[-.\s]??\d{4}\b
123-456-7890
(123) 456-7890
123.456.7890
"You can reach us at 123-456-7890 or (123) 456-7890"
["123-456-7890", "(123) 456-7890"]
\b\d{1,2}[\/.-]\d{1,2}[\/.-]\d{2,4}\b
12/31/2024
31-12-2024
2024.12.31
"The event is scheduled for 12/31/2024 or 31-12-2024"
["12/31/2024", "31-12-2024"]
\$\d+(?:\.\d{2})?
$19.99
$100
"The price of the product is $19.99 or you can buy it in bulk for $100"
["$19.99", "$100"]
\b(marketing|sales|advertising|promotion)\b
marketing
sales
advertising
promotion
"Our marketing and sales teams are collaborating on the new advertising campaign"
["marketing", "sales", "advertising"]
\d
[for matching multiple digits at once like 1234 use: \d+
]"There are 3 apples"
"There are 100 apples"
\ba\w*
"apple"
"mango"
cat
matches the string “cat”..
: Matches any single character except newline.+
: Matches one or more occurrences of the preceding element.?
: Matches zero or one occurrence of the preceding element.^
: Matches the start of a string.$
: Matches the end of a string.[]
: Matches any one of the characters within the brackets.|
: Acts as a logical OR between patterns.\\
: Escapes a special character to be treated as a literal.()
to group parts of a regex and capture them for later use.
(abc)+
matches one or more occurrences of the sequence “abc”.(?:...)
to group parts of a regex without capturing.
(?:abc)+
matches one or more occurrences of “abc” but does not capture the group.\\d(?=px)
matches digits followed by “px” but does not include “px” in the match.(?<=\\$)\\d+
matches digits preceded by "" in the match.