Written by:
Regex, short for regular expressions, is a powerful tool used for text processing and manipulation in programming. It allows developers to define patterns of characters that can be used to match specific strings of text. This can be incredibly useful for tasks such as data validation, parsing, and searching. Regex is used in a wide range of programming languages, including Javascript, Python, and Ruby. For those looking to learn more about regex, a regex tutorial is a great place to start. A good tutorial will cover the basics of regex syntax, provide examples of common use cases, and offer best practices for working with regular expressions. By learning regex, programmers can enhance their skills and write more efficient and effective code.
A RegEx pattern is a set of rules applied to match a certain data, let's say an email address, phone number, url and a long list of etc. Everything that follows a strict pattern can be translated into a RegEx pattern.
Creating a RegEx pattern can be tricky, if you're not familiarized with Regular Expressions, so to make our lives easier, here you'll find a list of examples for most used data patterns.
Emails are one of the data we receive and have to validate, that's why we are stating our examples with an email pattern that will match any valid email inside a text.
Javascript
1// We receive a text like this, and we want to extract the email address 2const text = `My name is Geek and my email address is geek@4geeksacademy.com`; 3 4const findEmail = (str) =>{ 5 const regex = /\b[\w.!#$%&’*+\/=?^`{|}~-]+@[\w-]+(?:\.[\w-]+)*\b/mg; 6 return str.match(regex) 7} 8 9console.log(findEmail(text)) 10//Output -> [geek@4geeksacademy.com]
Python
1dataset = "My name is Geek and my mail address is geek@4geeksacademy.com"; 2regex = r"\b[\w.!#$%&’*+\/=?^`{|}~-]+@[\w-]+(?:\.[\w-]+)*\b" 3 4def findEmail(term, text): 5 match = re.search(term, text) 6 return match 7 8print(findEmail(regex, dataset)) 9#Output -> <re.Match object; span=(39, 61), match='geek@4geeksacademy.com'>
Javascript
1// Check valid email 2const text = `geek@4geeksacademy.com`; 3 4const validEmail = (str) =>{ 5 const regex = new RegExp("^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$") 6 return regex.test(str) 7} 8//Output: true
Since we are checking if the email address has the correct pattern, we are using the test
method to receive a true/false
response.
Python
1#Check valid email 2dataset = "geek@4geeksacademy.com" 3regex = r"^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$" 4def validEmail(term, text): 5 match = bool(re.search(term, text)) 6 return match 7 8print(validEmail(regex, dataset)) 9#Output -> True
Since we are checking if the email address has the correct pattern, we are using the bool
to receive a true/false
response.
Javascript
1//Check integer 2const text = `15 312.3 456,7 512/4`; 6 7const checkInt = (str) =>{ 8 const regex = /^(\d+)$/gm; 9 return str.match(regex) 10} 11 12console.log(checkInt(text)) 13//Output -> [15]
Python
1#Check integer 2number = ("15\n" 3 "12.3\n" 4 "56,7\n" 5 "12/4") 6regex = r"^(\d+)$" 7def checkInt(term, text): 8 dataset = "".join(text) 9 match= re.search(term, dataset, re.MULTILINE) 10 return match 11 12print(checkInt(regex, number)) 13#output -> <re.Match object; span=(0, 2), match='15'>
Javascript
1#Check float 2const text = `15 312.3 456,7 512/4`; 6 7const checkFloat = (str) =>{ 8 const regex = /^(\d*)[.,](\d+)$/gm; 9 return str.match(regex) 10} 11 12console.log(checkFloat(text)) 13//Output -> [ '12.3', '56,7' ]
Python
1#Check Float 2number = ("15\n" 3 "12.3\n" 4 "56,7\n" 5 "12/4") 6regexFloat = r"^(\d*)[.,](\d+)$" 7def checkInt(term, text): 8 dataset = "".join(text) 9 match= re.findall(term, dataset, re.MULTILINE) 10 return match 11 12print(checkInt(regexFloat, number)) 13#output -> [('12', '3'), ('56', '7')]
We are using the findall()
method because we have more than one in our dataset, if you would only want one result, use the search()
method instead. The output would be <re.Match object; span=(3, 7), match='12.3'>
Javascript
1const text = `15 212.3 356,7 412/4`; 5 6const checkDecimals = (str) =>{ 7 const regex = /^(\d+)[\/](\d+)$/gm; 8 return str.match(regex) 9} 10 11console.log(checkDecimals(text)) 12//Output -> [ '12/4' ]
Python
1#Check decimals 2number = ("15\n" 3 "12.3\n" 4 "56,7\n" 5 "12/4") 6 7regexDecimals = r"^(\d+)[\/](\d+)$" 8def checkDecimals(term, text): 9 dataset = "".join(text) 10 match= re.search(term, dataset, re.MULTILINE) 11 return match 12 13print(checkDecimals(regexDecimals, number)) 14#Output -> <re.Match object; span=(13, 17), match='12/4'>
A strong password consists on a minimum length of 6 characters and at least:
Javascript
1#Check password 2const goodPass = `%G[e](e)?k@1&3$` 3const badPass = ` 4geek@123 5GEEK@123 6Geeks123 7Geeks@Geeks` 8 9const checkPass = (pass) =>{ 10const regex = /(?=^.{6,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!@\[\]\(\)?$%&\/\(\)\?\^\'\\\+\-\*]))^.*/gm; 11 return regex.test(pass) 12} 13console.log(checkPass(goodPass)) //Output -> true 14console.log(checkPass(badPass)) //Output -> false
Python
1goodPass = "%G[e](e)?k@1&3$" 2badPass = ("geek@123\n" 3"GEEK@123\n" 4"Geeks123\n" 5"Geeks@Geeks") 6regex = r"(?=^.{6,}$)((?=.*\w)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[|!\"$%&\/\(\)\?\^\'\\\+\-\*]))^.*" 7def checkPassword(term, text): 8 match = bool(re.search(term, text, re.MULTILINE)) 9 return match 10 11print(checkPassword(regex, goodPass)) #Output -> True 12print(checkPassword(regex, badPass)) #Output -> False
In both cases, we are returning True/False
if the password provided matches the requirements.
Javascript
1// Check url 2const checkUrl = (url) =>{ 3 const regex = /^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$/gm; 4 return regex.test(url) 5 } 6 7console.log(checkUrl("www.4geeks.com")) //Output -> true 8console.log(checkUrl("4geeks.com")) //Output -> true 9console.log(checkUrl("http://www.google.com")) //Output -> true 10console.log(checkUrl("4geeks")) //Output -> false
Python
1#check url 2def checkUrl(url): 3 regex = r"^(((https?|ftp):\/\/)?([\w\-\.])+(\.)([\w]){2,4}([\w\/+=%&_\.~?\-]*))*$" 4 match= bool(re.search(regex, url)) 5 return match 6 7print(checkUrl("www.4geeks.com")) #Output -> True 8print(checkUrl("4geeks.com")) #Output -> True 9print(checkUrl("http://www.google.com")) #Output -> True 10print(checkUrl("4geeks")) #Output -> False
In both cases we are returning a boolean (true/false
) if the URL is valid.
You can learn more about this topic and others related at 4Geeks. Hope you enjoyed the reading and keep on the Geek side!