I. What is RegEx?
II. Common uses of RegExes
III. Benefits of using RegExes
III. Examples of Metacharacters of Regular Expressions
RegEx or A Regular Expression is a pattern to match given texts. RegEx allows us to define abstract strings or structured texts, to check and to see if they match the other strings or not. RegExes can be unscrupulously complicated and may be eminently intricate and hard to follow but are very useful because they allow us to accomplish a lot through pattern matching.
I'll try to show examples to understand how powerful RexExes are using the re
module of python
Validating data such as a phone number that only has numbers, brackets and dashes. An email address that looks for any combination of A - Z in both upper and lower cases which allows a few other special characters such as: ! # $ % & ' * + - / = ? ^ _
{ |`
import re
regex = '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$'
def check(email):
if(re.search(regex,email)):
print("Valid Email")
else:
print("Invalid Email")
ii. Email validation in Javascript:
function ValidateEmail(inputText)
{
var mailformat = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$/
if(inputText.value.match(mailformat))
{
alert("This is not a valid email address");
return false;
}
}
iii. Email validation in Go
package main
import (
"fmt"
"regexp"
)
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func main() {
e := "[email protected]"
if isEmailValid(e) {
fmt.Println(e + " is a valid email")
}
if !isEmailValid("just text") {
fmt.Println("not a valid email")
}
}
func isEmailValid(e string) bool {
if len(e) < 3 && len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}
Replacement: To find and replace a word or set of words with other characters.
Transforming a title to be a url slug
>>> content = "This is a title."
>>> slug = content.replace(' ', '-').lower()
>>> slug
'this-is-a-title.'
>>> content = "This is a title."
>>> slug = content.toLocaleLowerCase().replace(' ', '-')
'this-is-a-title'
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(
// "This is a title"
strings.ToLower("This is a title."),
strings.Replace("this is a title", " ", "-", 4),
)
}
this is a title. this-is-a-title
Scrapping: Finding occurrences of text in a website, pdf, excel/CSV and the likes.
String parsing to retrieve data from structured strings such as URLs or logs.
“Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems." – Jamie Zawinski Excerpt From: Jaime Buelta. “Python Automation Cookbook.” Apple Books.
Keep it simple, stupid.