List of Contents:
- Introduction
- Regex for Matching Numbers
- Regex for Matching String
- Regex for Search and Replace String
- Regex to Search the Content in the File
- Regex to Modify the Content of a File
- Regex for Validating the Email Address
- Regex for Validating the Phone Number
- Regex for Validating the Username and Password
Introduction
Some commonly used special characters that are used in the regular expression pattern to match or search and replace the string are mentioned in the following:
Character | Purpose |
. | It is used to search for any character except the newline. |
^ | It is used to search at the beginning of the text. |
$ | It is used to search at the end of the text. |
\b | It is used to search for a backspace character. |
\n | It is used to search for a newline character. |
\t | It is used to search for a tab character. |
\r | It is used to search for a carriage return character. |
\d | It is used to search for the digit character. |
\D | It is used to search for the non-digit character. |
\s | It is used to search for the whitespace character. |
\S | It is used to search for the non-whitespace character. |
Some commonly used regular expression patterns that are used to search different types of range of characters are mentioned in the following:
Range | Purpose |
[a-z] | It is used to search all lowercase alphabetic characters. |
[A-Z] | It is used to search all uppercase alphabetic characters. |
[0-9] | It is used to search all numeric characters. |
[a-zA-Z] | It is used to search all alphabetic characters. |
[0-9a-zA-Z] | It is used to search all alphanumeric characters. |
[^A-Z] | It is used to search all characters except the uppercase characters. |
[^0-9] | It is used to search all characters except the numeric characters. |
Regex for Matching Numbers
The use of [0-9] expression is shown in the following script to check whether the input value is a number or not. If the input that is taken from the user is a number, a success value is printed. Otherwise, an error message is printed.
#Take input from the user
read -p "Enter your age: " age
#Check whether the input value is valid or invalid
if [[ $age =~ [0-9] ]]; then
#Print message for the valid data
echo "$age is valid age value."
else
#Print message for the invalid data
echo "Input value must be a number."
fi
The following output appears after executing the script with the input value of 70:
The following output appears after executing the script with the input value of “test”:
Regex for Matching String
The use of [a-zA-Z] expression is shown in the following script to check whether the input value contains all alphabet characters or not. If the input that is taken from the user contains all alphabetic characters, a success value is printed. Otherwise, an error message is printed.
#Take input from the user
read -p "Enter the book name: " bname
#Check whether the input value is valid or invalid
if [[ $bname =~ [a-zA-z] ]]
then
#Print message for the valid data
echo "$bname is valid data."
else
#Print message for the invalid data
echo "Input value must be a string."
fi
The following output appears after executing the script with the value of “Shell Programming”:
The following output appears after executing the script with the value of 230:
Regex for Search and Replace String
The following script shows the use of the regular expression pattern for searching and replacing the particular portion of the string value. A string value is taken from the user. If the input string contains any character from “a” to “m”, those characters will be replaced by the “_”character. The “/[a-m]/” pattern is used in the script to search the characters. Then, both the original string and the replacement string are printed in the output.
#Take input from the user
read -p "Enter a string value: " Str
#Modify the string based on the pattern
modified_Str=${Str//[a-m]/_}
#Print the main string value
echo "The original string is $Str"
#Print the replaced string value
echo "The modified string is $modified_Str"
The following output appears after executing the script with the input value of “hello”. Here, four characters of the string value are replaced by the “_”character. The replaced string is “____o”:
The following output appears after executing the script with the input value of “Zoo”. No matching character is found in the input value. So, the original string was not modified:
Regex to Search the Content in the File
Create a text file with the named “products.txt” with the following content before checking the script of this example.
products.txt
1090 Monitor 50
2378 Mouse 10
4521 Printer 100
5682 Keyboard 25
The “grep” command is one of the options to search a particular string in the file in Bash from the terminal. The following “grep” command searches those lines of the “products.txt” file that end with “00”.
The following output appears after executing the “grep” command. One line exists in the file that matches with the pattern and that line is printed in the output:
The method of searching the particular lines from a file using a regular expression in the Bash script is shown in the following script. The filename is taken from the command-line argument value and the content of the file is read using the “while” loop if any line contains any character from “L” to “M”. Then, that line is printed in the output.
#Read the file line by line
while read -r line
do
#Search the lines that contain the characters between 'L' to 'M'
if [[ $line =~ [L-M] ]]
then
#Print the matching line
echo $line
fi
#Read the file that is passed in the first argument
done < $1
The following output appears after executing the script with the argument value of “products.txt” file. This file contains two lines that match the pattern:
Regex to Modify the Content of a File
Multiple Bash commands exist in Bash to search and replace the content of a file. The “sed” command is one of them. The following script takes the filename from the command-line argument. The “search” string and the “replace” string are taken from the user. If the input values are non-empty and the filename that is passed as the command-line argument value contains any string that matches with the search word, the matching string will be replaced by the “replace” string. The “sed” command with the “-i” option and search and replace patterns are used in the script to modify the content of the file.
#Read the filename
filename=$1
#Take the string for searching
read -p "Enter the search string: " src
#Take the string for replacing
read -p "Enter the replace string: " rep
if [[ ! -z $src && ! -z $rep ]];
then
#Search and replace the string in the file
sed -i "s/$src/$rep/" $filename
fi
Run the following command to check the content of the “products.txt” file. According to the output, the file contains five lines:
Run the following command to execute the script by giving the “products.txt” file as the first command-line argument that was created earlier. According to the output, the “keyboard” is taken as the search string, and the “scanner” is taken as a replacement string. The “search” string exists in the “products.txt” file. So, the content of the file will be modified.
Next, run the previous “cat” command again to check the content of the “products.txt” file. According to the output, the “keyboard” word is replaced by the “Scanner” word.
Regex for Validating the Email Address
One of the main purposes of using the regular expressions is to validate the data. Very simple uses of regular expression patterns are shown in the previous examples. The complexity of the pattern will be increased based on the type of validation. You have to write a long regular expression pattern to validate an email address. The following script shows the method to validate an email address using a regular expression pattern. An email address contains multiple parts. The first part contains the username which may contain any alphanumeric characters, dot(.), underscore(_), and hyphen(-). Next, “@” is used in the email. The last part of the email contains the domain name that can end with two to four characters. According to the script, an email address is taken from the user and the email is valid or invalid and is checked by a regular expression pattern.
#Take an email address from the user
read -p "Enter your email address: " em
#Check whether the email address is valid or invalid
if [[ "$em" =~ ^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]
then
#Print message for the valid email address
printf "%s is a valid email address.\n" $em
else
#Print message for the invalid email address
printf "%s is an invalid email address.\n" $em
fi
The following output shows that “[email protected]” is a valid email address because the “if” statement returns true when it is matched with the given regular expression pattern:
The following output shows that “[email protected]” is an invalid email address because the “if” statement returns false when it is matched with the given regular expression pattern:
Regex for Validating the Phone Number
The method of validating a phone number with the “880-XXX-XXX-XXXX” format using a regular expression is shown in the following script. Here, the “X” indicates any digit. The phone number is taken from the user. According to the script, a phone number is taken from the user and the phone number is valid or invalid and is checked by a regular expression pattern.
#Take a phone number from the user
read -p "Enter your phone number: " phone
#Check whether the phone number is valid or invalid
if [[ "$phone" =~ ^880-[0-9]{3}\-[0-9]{3}\-[0-9]{4}$ ]]
then
#Print a message for the valid phone number
printf "%s is a valid phone number.\n" $phone
else
#Print a message for the invalid phone number
printf "%s is an invalid phone number.\n" $phone
fi
The following output shows that “880-169-345-7840” is a valid phone number because the input phone number matches the regular expression pattern:
The following output shows that “880-176-6789-89” is an invalid phone number because the input phone number does not match the regular expression pattern:
Regex for Validating the Username and Password
The method of validating the username and password values is shown in the following script. According to the script, the username may contain lowercase letters and digits only and the length of the username must be 6 to 10 characters long. The password may contain any alphanumeric characters, “$”, “#”, “@”, “_”, and “-“ characters only, and the length of the password must be 5 to 8 characters long.
#Take username and password
read -p "Enter username: " un
read -p "Enter username: " pw
#Check whether the username and password is valid or invalid
if [[ "$un" =~ [a-z0-9]{6,10} && "$pw" =~ [a-zA-Z0-9#$@_-]{5,8} ]]
then
#Print a message when both input values are valid
echo "Username and password are valid."
else
#Print a message if any of the input value is invalid
echo "Username or password are invalid."
fi
The following output appears after executing the script with the input values of “fahmida19” as a username and “hello@123” as a password:
Conclusion
Different uses of the regular expression patterns are shown in this tutorial using multiple Bash commands and scripts. The methods of searching, replacing, and validating the data using simple to complex regular expression patterns are shown here.