FUNDAMENTALS A Complete Guide for Beginners
Regex or regular expression is a sequence of characters that specifies a search pattern. Matching with regex can be done by incorporating regex with an if statement that involves comparing a string against a pattern determined by the regular expression. In such circumstances, regex is used within the condition of the ‘if statement’. This operation is hugely used in checking the validity of input, parsing text from big strings, extracting necessary data, and searching.
What is Regex?
Regex or Regex matching is an operation to look for similarity between the specified pattern and target string. The pattern is usually a combination of characters and special characters. There are some metacharacters in regex that carry special meaning. Some of these characters are:
- ‘*’: Matches one or more occurrences of the preceding character or group.
- ‘.’(Dot): Matches any single character.
- ‘^’: Anchors the regex at the beginning of the line.
- ‘$’: Anchors the regex at the end of the line.
4 Cases of Regex Use in Bash If Condition
Regex can be used to find a specific file extension, match a substring within a string irrespective of case, or use any kind of negated regex to find anything except the original string. In the following section, I will discuss 4 use cases of regex within the ‘if condition’ in bash:
1. Using Regex Inside an If Clause
Regular expression or regex is an approach to accomplish some special task by giving expression-based commands. For example, you can search for a specific type of file using the following syntax: if [[ $file =~ \.(extension)$ ]]
. Here the . (dot) matches any single character before the specified extension with dot.
For a practical approach check out the below script to find if a file is with the .txt extension:
#!/bin/bash
filename="filex.txt"
if [[ "$filename" =~ \.txt$ ]]; then
echo "Filename has a .txt extension"
else
echo "Filename does not have a .txt extension"
fi
After assigning the filename to the filename variable, the if [[ "$filename" =~ \.txt$ ]]
command looks for a match of the .txt inside the filename variable. Then print a message according to the return status of the if condition.
The script has found a .txt extension inside the filename.
if [[ $filename =~ \.(txt|csv)$ ]]
. Here the txt and the and csv extension are piped together and passed after the . (dot) character. 2. Using Regex in Case-Insensitive Matching
Case-insensitive matching means looking for a match irrespective of the case. First, execute the shopt command with option -s nocasematch to enable case-insensitive matching. Then to perform the matching operation of the pattern inside the variable_name use the syntax: $variable_name =~ ^pattern$
as the condition inside the if statement.
Here is the bash script on how to find case insensitive match:
#!/bin/bash
name="Susmit"
shopt -s nocasematch
if [[ $name =~ ^susmit$ ]]; then
echo "The name is Susmit (case-insensitive)."
else
echo "The name is not Susmit (case-insensitive)."
fi
The shopt -s nocasematch
command enables case-insensitive search. After that the $name =~ ^susmit$
inside the if condition looks for a match inside the name variable.
A case-insensitive match has been found between the assigned pattern and the string inside the name variable.
3. Using a Negated Regex in If Condition
The negated regex matches everything except the original pattern. To achieve this, the syntax used inside the if condition for such regex is: if ! [[ "$string_where_to_search" =~ [original pattern] ]];
For instance, to verify if a string contains any number, inside the string_where_to_search, the necessary command incorporating appropriate negated regex is: ! [[ "$string" =~ [0-9] ]];
.
Here is the complete bash script to check the existence of a number in a string:
#!/bin/bash
string="Hello, world!"
if ! [[ "$string" =~ [0-9] ]]; then
echo "String does not contain any digits"
else
echo "String contains at least one digit"
fi
After assigning the string variable the if ! [[ "$string" =~ [0-9] ]];
condition checks whether the string variable contains any number. Finally, prints a message according to the result.
The bash script does not find any number or digit in the string.
4. Using Regex with Variables in If Condition
Regex can be saved in a variable and this variable can be used inside the condition of the if clause by following the syntax: "string" =~ $pattern
. Now I will save a pattern inside a variable using variable_name="pattern"
and then use this variable inside the condition of an if clause following the syntax: "hello" =~ $variable_name.
Here is the bash script to achieve this:
#!/bin/bash
pattern="[a-z]+"
if [[ "hello" =~ $pattern ]]; then
echo "String matches the regex pattern"
else
echo "String does not match the regex pattern"
fi
The pattern=”[a-z]+” command assigned a pattern to the pattern variable. After that, the “hello” =~ $pattern condition inside the if clause looks for a match of pattern inside the “hello”. Finally prints a message according to the search result.
See, the script has found a match between the string and the assigned pattern inside the variable.
Conclusion
To conclude, regex is a vital tool for searching the intended pattern within the string utilizing the if condition in Bash. Here in this article, I have covered 4 different cases of using regex in if conditions. If you have any queries or questions regarding this article please comment below. Happy Scripting!
People Also Ask
Why use regex?
The regular expressions (regex) are especially helpful for the filtering purpose. To make a filter more specific or more general, regular expressions are used which are collections of characters that specify a pattern of text to be matched.
What is the use of regex if statement in Bash?
The if statement of the bash script can match text patterns with regex using =~ and double square brackets [[ ]]. To look for patterns that don’t match, you can also utilize negative regexes. Variables can be used to hold regex patterns for usage in if statements. Moreover, regex is an effective tool for pattern matching in Bash because of these qualities.
How to compare two strings in Bash regex?
To compare strings with regular expressions in Bash, use the “grep” command with the “-E” (extended regex) option. Sample script is below:
#!/bin/bash
string="Linux Simply"
if echo "$string" | grep -E "^Linux"; then
echo "Match found"
else
echo "Match not found"
fi
If the “grep” command searches a string for a pattern and returns a success exit code 0 if the pattern is found and a failure exit code 1 if the pattern is not found.
How do you check if a string is empty in bash?
To check if a string is empty in a Bash script, use the -z conditional which returns 0 if the length of the string is 0 and 1 if it is greater than 0.
Sample script for this purpose is below:
#!/bin/bash
Var="hello"
if [[-z "$Var"]]; then
echo "Var is empty"
else
echo "Var is not empty"
fi
What is a pattern in regular expression?
A regular expression is a pattern that an input text’s regular expression engine looks for matches in. A pattern is made up of one or more structures, operators, or literal characters.
Related Articles
- Check If a Variable Exists in Bash If Statement
- Check If An Input Argument Exists in Bash [4 Methods]
- Check If a String Contains Substring, Number & Letter in Bash
- Check If a String is Empty or Not in Bash [5 Methods]
- How to Compare Strings in Bash With if Statement [6 Methods]
- How to Compare Numbers in Bash With If Statement [2 Methods]
- Check If Array Contains an Element in Bash [6 Methods]
- Bash if-else Condition in One-Line [2 Ways]
- Using grep in if Statement in Bash [4 Cases]
- Bash rm If Exists [All Case Explained]
- What is ‘$?’ in the Bash if Statement? [8 Examples]
<< Go Back to If Else in Bash | Bash Conditional Statements | Bash Scripting Tutorial
Hi,
> point 1 (\. : Here the . (dot) matches any single character )
No, there \. matches exact character ‘dot’ instead.
Hi!
I guess you missed the explanation part. The “\.” expression will match any character before the extension with the dot.