The common expression is a really useful gizmo to match any content material or search and exchange the content material of a file or in a string utilizing a regex sample. It may be used with the Bash script in several methods. The =~ image is used within the “if” assertion of Bash to look any string. Many kinds of expression can be utilized to outline the matching regex patterns. Among the generally used common expressions and the makes use of of some expressions with the =~ operator are defined on this tutorial.
Generally Used Common Expressions
Expression | Function |
. | It’s used to look the characters with no newline (n). |
^ | It’s used to seek for characters firstly of the string. |
$ | It’s used to seek for characters on the finish of the string. |
[0-9] | It’s used to look any quantity from the vary of 0-9 within the string. |
[A-Z] | It’s used to seek for any character from the vary of A-Z within the string. |
[a-z] | It’s used to seek for any character and quantity from the vary of a-z within the string. |
[^A-Z0-9] | It’s used to look all characters besides the capital alphabets and digits within the string. |
[a-zA-z0-9] | It’s used to seek for any character and quantity from the vary of a-z, A-Z, and 0-9 within the string. |
n | It’s used to seek for the newline character. |
t | It’s used to look the tab character. |
Completely different Examples of =~ Operators
The alternative ways of looking out a selected string inside a textual content utilizing the =~ operator and common expression sample are proven on this a part of the tutorial.
Instance 1: Search a Explicit String Utilizing the “*” Image
Create a Bash file with the next script that takes the primary string worth the place the string is searched and the search string worth is searched in the primary string worth. Subsequent, the “=~” operator is used with the search string to verify if the search string exists in the primary string or not. Right here, the “*” image is used to indicate any variety of characters.
#Take the primary string
learn -p “Enter the primary string worth: “ strValue
#Take the search string
learn -p “Enter the search string worth: “ search
#Verify whether or not the search string exists in the primary string or not
if [[ $strValue =~ .*$search.* ]]; then
echo “The string exists within the textual content.”
else
echo “The string would not exist within the textual content.”
fi
The next output seems after executing the script with the primary string worth of “Study common expression” and the search string worth of “common”. Right here, the search string exists in the primary string:
Instance 2: Verify the Extension of the Explicit File
Create a Bash file with the next script that takes the filename from the command-line argument and verify whether or not the file is a Bash file or not.
#Take the filename from the argument
filename=$1
#Outline the looking out extension worth
extension=‘bash’
#Verify if the extension matches with the file extension or not
if [[ “$filename“ =~ .$extension$ ]]; then
echo “$filename is a bash file.”
else
echo “$filename shouldn’t be a bash file.”
fi
The next output seems for the “ping1.bash” filename which is a Bash file:
The next output seems for the “hi there.txt” filename which isn’t a Bash file:
Instance 3: Search the Explicit Characters in a String
Create a Bash file with the next script that takes a string worth and search the vary of characters from “a” to “e” within the string.
#Take the primary string
learn -p “Enter the primary string worth: “ strValue
#Verify if the string accommodates any character from a to e or not
if [[ $strValue =~ [a-e] ]]; then
echo “The string accommodates characters from ‘a’ to ‘e'”
else
echo “The string doesn’t comprise any character from ‘a’ to ‘e'”
fi
The next output seems after executing the script with the “LinuxHint” enter worth:
The next output seems after executing the script with the “Good day World” enter worth:
Instance 4: Validate the Cellular Quantity
Create a Bash file with the next script that takes a cell variety of the actual format and verify whether or not the quantity is legitimate or invalid utilizing the common expression sample and the =~ operator.
learn -p “Enter a cell quantity [880-XXXX-XXXXXX]: “ cell
#Set the sample for matching
regexPattern=‘^880-[0-9]{4}-[0-9]{6}’
#Verify the cell quantity is legitimate or invalid
if [[ $mobile =~ $regexPattern.* ]]; then
echo “Cellular quantity is legitimate.”
else
echo “Cellular quantity is Invalid.”
fi
The next output seems after executing the script with the “880-1922-032970” enter worth which is legitimate:
The next output seems after executing the script with the “880-15677-67345” enter worth which is invalid:
Conclusion
The strategies of utilizing the “=~” operator to look the string values with several types of common expressions are proven on this tutorial.