Written by:
Regular expressions are a powerful tool for matching text patterns and finding specific letters or words. In the following example, we will use a regular expression to find all words within a string in two of the most important programming languages Python and Javascript. You can find a very interesting RegEx Tutorial at 4Geeks's Blog.
1import re 2 3regex_pattern = r"[a-zA-Z]+" 4string_text = "Hello World 123" 5 6matched_words = re.findall(regex_pattern, string_text) 7print(matched_words) # output: ['Hello', 'World']
1const regexPattern = /[a-zA-Z]+/g; 2const stringText = "Hello World 123"; 3 4const matchedWords = stringText.match(regexPattern); 5console.log(matchedWords); // output: ['Hello', 'World']
In these examples, we use the regex pattern [a-zA-Z]+
to find all words within a string, this pattern searches for all words in the text "Hello World 123" regardless of whether the letters are lowercase or uppercase. This is a very simple example of how to use regex to find words within a string, in both examples the words that match the pattern are stored in the matched_words
(Python) and matchedWords
(Javascript) variables.
There are many ways to search for letters, words, or text patterns with regular expressions. Whether you want to find all the words within a string or search for a word that starts with a capital letter, or you want to search for a specific text structure, you can always use a regex pattern to search for any type of word or letter you might need.
Here are some examples of regex patterns you can use to search for words or letters.
This regular expression can be used to search for any capitalized word within a string.
1"\b[A-Z]\w*"
1import re 2regex_pattern = r"\b[A-Z]\w*" 3string_text = "This Is an Example 123" 4 5matched_words = re.findall(regex_pattern, string_text) 6print(matched_words) # output: ['This', 'Is', 'Example']
1const regexPattern = /\b[A-Z]\w*/g; 2const stringText = "This Is an Example 123"; 3 4const matchedWords = stringText.match(regexPattern); 5console.log(matchedWords); // output: ['This', 'Is', 'Example']
In this example, we use the regex pattern \b[A-Z]\w*
to search for any word starting with a capital letter, this will return an array with the capitalized words that are stored in the matched_words
(Or matchedWords
on Javascript) variable in both code examples.
Regular expression to find words within a string.
1"\b[A-Za-z]+\b"
1import re 2regex_pattern = r"\b[A-Za-z]+\b" 3string_text = "This is an Example text 123 456 789" 4 5matched_words = re.findall(regex_pattern, string_text) 6print(matched_words) # output: ['This', 'is', 'an', 'Example', 'text']
1const regexPattern = /\b[A-Za-z]+\b/g; 2const stringText = "This is an Example text 123 456 789"; 3 4const matchedWords = stringText.match(regexPattern); 5console.log(matchedWords); // output: ['This', 'is', 'an', 'Example', 'text']
Here we use the regex pattern \b[A-Za-z]+\b
to find all the words within a string regardless of whether it is upper or lower case, in both examples, the regex returns an array with the words found and these are stored in the matched_words
(Or matchedWords
on Javascript) variable.
Regular expression to find words beginning with a letter.
1"\b[a-zA-Z]\w*\b"
1import re 2regex_pattern = r"\b[a-zA-Z]\w*\b" 3string_text = "This a 4Geeks regex example abc123 123abc" 4 5matched_words = re.findall(regex_pattern, string_text) 6print(matched_words) # output: ['This', 'a', 'regex', 'example', 'abc123']
1const regexPattern = /\b[a-zA-Z]\w*\b/g; 2const stringText = "This a 4Geeks regex example abc123 123abc"; 3 4const matchedWords = stringText.match(regexPattern); 5console.log(matchedWords); // output: ['This', 'a', 'regex', 'example', 'abc123']
In this example, we use the regex pattern\b[a-zA-Z]\w*\b
to search for words beginning with a letter regardless of whether it's uppercase or lowercase. The matching words are stored in the matched_words
(Or matchedWords
on Javascript) variable.
Regular expression to find words ending in a letter.
1"\b\w*[A-Za-z]\b"
1import re 2regex_pattern = r"\b\w*[A-Za-z]\b" 3string_text = "This a 4Geeks regex example abc123 123abc" 4 5matched_words = re.findall(regex_pattern, string_text) 6print(matched_words) # output: ['This', 'a', '4Geeks', 'regex', 'example', '123abc']
1const regexPattern = /\b\w*[A-Za-z]\b/g; 2const stringText = "This a 4Geeks regex example abc123 123abc"; 3 4const matchedWords = string_text.match(regexPattern); 5console.log(matchedWords); // output: ['This', 'a', '4Geeks', 'regex', 'example', '123abc']
The regex pattern \b\w*[A-Za-z]\b
in this example is used to search for all the words ending with a letter, excluding those ending with a number, a symbol, or anything else. Those that meet the regex pattern are stored in the matched_words
(Or matchedWords
on Javascript) variable.
This Regex pattern searches for all words containing at least one uppercase letter and one lowercase letter.
1"\b(?=\S*[a-z])(?=\S*[A-Z])\S+\b"
1import re 2regex_pattern = r"\b(?=\S*[a-z])(?=\S*[A-Z])\S+\b" 3string_text = "This is an Example Sentense, It contains word thaT are both in UPPERCASE and lowercase letter" 4 5matched_words = re.findall(regex_pattern, string_text) 6print(matched_words) # output: ['This', 'Example', 'Sentense', 'It', 'thaT']
1const regexPattern = /\b(?=\S*[a-z])(?=\S*[A-Z])\S+\b/g; 2const stringText = "This is an Example Sentense, It contains word thaT are both in UPPERCASE and lowercase letter"; 3 4const matchedWords = stringText.match(regex_pattern); 5console.log(matchedWords); // output: ['This', 'Example', 'Sentense', 'It','thaT']
Here we use the regex pattern \b(?=\S*[a-z])(?=\S*[A-Z])\S+\b
to search for all words within a string containing at least one uppercase and one lowercase letter. All the words that meet the regex pattern are stored in the matched_words
(Or matchedWords
on Javascript) variable.
These are some real examples where you can use Regex patterns to search for a specific letter, word, or written structure.
An interesting way to use a regex letter pattern is to confirm that a URL starts with a correct http
request.
^https?:\/\/
1import re 2 3regex_pattern = r"^https?:\/\/" 4api_url = "https://example.com" 5 6if re.search(regex_pattern, api_url): 7 print(f"The url '{api_url}' is a valid link") 8else: 9 print(f"The url '${api_url}' has an incorrect format")
1const regex_pattern = /^https?:\/\//; 2const api_url = "https://example.com"; 3 4if (api_url.match(regex_pattern)) { 5 console.log(`The url "${api_url}" is a valid link`); 6} else { 7 console.log(`The url "${api_url}" has an incorrect format`); 8}
In this example, we use the regex pattern ^https?:\/\/
to check if a URL starts with a correct http secure protocol structure, this can be useful when you are working with an API and you want to make sure that the URL or endpoint you are using has a correct structure and this will help you to avoid error in your application.
Another way to use regex letter patterns is to confirm that the URL of an image ends with a correct and valid image extension.
.jpg
or .png
or .svg
\.(jpg|png|svg)$
1import re 2 3regex_pattern = r"\.(jpg|png|svg)$" 4image_url = "https://image-example.jpg" 5 6if re.search(regex_pattern, image_url): 7 print(f"The image extension '{image_url}' is valid") 8else: 9 print(f"The image extension '{image_url}' is incorrect")
1const regexPattern = /\.(jpg|png|svg)$/; 2const imageUrl = "https://image-example.jpg"; 3 4if (imageUrl.match(regexPattern)) { 5 console.log(`The image extention "${imageUrl}" is valid`); 6} else { 7 console.log(`The image extention "${imageUrl}" is incorrect`); 8}
In this example, we use the regex pattern \.(jpg|png|svg)$
to confirm the extension of an image is correct and valid, this pattern can be useful when you are working on a project like an image gallery and you want to make sure that the images uploaded by users have a correct extension.
Regular expressions can be used to search for any type of pattern, in this article we mention some examples of how to use Regex to find characters or words within a string, there are many ways to use Regex patterns to search for letters or words whether you want to find all the word that starts with an uppercase letter or if you want to search for words that end with a specific pattern or find all the words in a string regardless of whether they contain an uppercase or a lowercase letter.
If you are interested in learning more about regular expressions, I recommend you to visit this regular expression tutorial with examples from 4Geeks where you will find explanations for every Regex symbol, for example, what is the caret ^
symbol use for or the brackets []
, this page has very good explanations and examples that will help you to understand how the regular expression work.