Regular expressions (regex) are a very useful and powerful tool for any developer, they allow you to describe, match, and search for text patterns. In the following example, we will see how to use a regular expression to check if a sentence contains a specific word in both Python and JavaScript.
Loading... Loading...
In this example, we use the regular expression \b4Geeks\b
to search for the word (4Geeks) within a sentence. To use a regular expression in Python we need to import the module (re) and on this module call the search()
method, this method receives two parameters, the first one is the string on which we want to search the text pattern and the second one is the regular expression with the pattern to search. In JavaScript you can use the string method match()
to check if a phrase contains a certain pattern, this method receives the regular expression as a parameter.
As mentioned before, regular expressions can be very useful in many different situations. The following are some RegEx examples of the most common scenarios where you may need to use a regular expression to search or validate a text pattern.
Suppose you are working on an application and you want to verify that the phone number entered by the user has a valid format, for this, you can use the following regular expression.
Loading... Loading...
In this example, we used the regular expression to check if a phone number follows the pattern (000-000-0000), this pattern ensures that the number entered by the user has a valid format. The ^
symbol in the regular expression indicates that the match must be at the beginning of the string, the \d
pattern represents any number, the {3}
pattern represents how many times the digit must be repeated, and the $
symbol indicates that the match must also be at the end of the string.
Another way in which regular expressions can be handy to validate an email, validating an email is necessary in almost all applications that require login and registration and it is a very simple process to do with a regular expression.
Loading... Loading...
In this example, we use a regular expression to validate if an email has a valid format, this is necessary for almost all the applications that require authentication, and as you can see it is a quite simple process to achieve with a regular expression. Next, we will see what each part of the JavaScript regular expression is used for.
[a-zA-Z0-9._%+-]+
: This is a character set that matches any uppercase or lowercase letter, digit, period, underscore, percent, plus, or minus. The +
at the end means this character set must appear at least once.
@
: This is a literal character that must appear in the string.
[a-zA-Z0-9.-]+
: This is another character set that matches any uppercase or lowercase letter, digit, period, or hyphen.
\.
: This is an escape character that matches a literal period. Periods have a special meaning in regular expressions, so we need to escape them with a backslash to match a literal period.
[a-zA-Z]{2,}
: This character set matches any uppercase or lowercase letter. The {2,}
at the end means this character set must appear at least two times.
In some applications, you may need to validate whether a given URL is formatted correctly. This means that the URL has a valid domain and a valid structure. For this purpose, the best option is to use a regular expression.
Loading... Loading...
Here, we are using the regular expression stored in the regexPattern
variable to check if the URLs within the list (urls) have a valid format. This regular expression checks if a URL starts with (https)
or (www)
, and it also checks that at the end has a valid domain after a period.
Depending on the type of URL you need to validate, you may need to adjust the structure of the regular expression.
Validate a date format is another example where regular expressions can be very useful, for example, imagine that you need the date of birth of a user for an application and you need to verify that the date entered by the user follows a valid format, for this the best option is to use a regular expression.
Loading... Loading...
In this example, we used a regular expression to validate if the dates in the usersBirthdates
list have a valid format. This regular expression matches the format (DD/MM/YYYYYY) and validates if the day of the date is between 1 and 31, if the month is between 1 and 12, and also if the year is between 1900 and 2024 to make sure that the birth date is realistic and appropriate.
We can even use regular expressions to extract patterns from a string, in this case, we are using it to extract HTML tags.
Loading... Loading...
Here, we are using the regular expression <[^>]*>
to extract the HTML tags from the string stored in the text
variable and store the results in the htmlTags
variable. This can be very useful if you need to convert a string into HTML for a web page.
In this article we talked about regular expressions and how they can be used to perform different tasks, we also saw some examples of the most common cases where developers may need to use regular expressions. We hope this article has been helpful to you and that you have found the examples and tips on regular expressions particularly valuable for your Python and JavaScript development. We invite you to continue exploring other resources on our blog to further enhance your knowledge and technical skills. Ready to take your regular expression mastery to the next level? Sign up now for FREE at 4Geeks.com and gain access to a wide range of learning and development opportunities!