Skip to content

Regex shell scripting tutorial #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions regex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
#Purpose: regex examples
#Version: 1.0
#Create Date: Sun Nov 27 00:27:33 EST 2022
#Modified Date:

# START #

numString1="1234"
numString2="16789"
numString3="1579"


echo "Example 1"
if [[ $numString1 =~ ^1 ]]; then
echo "String \"$numString1\" starts with a \"1\", and matches regex: ^1"
fi

echo "Example 2"
if [[ $numString2 =~ ^1 ]]; then
echo "String \"$numString2\" starts with a \"1\", and matches regex: ^1"
fi

echo "Example 3"
if [[ $numString3 =~ ^1.7 ]]; then
echo "String \"$numString2\" starts with a \"1\", followed by any character, and followed by a 7. "
echo "This string matches the regex: ^1.7"
fi

echo "Example 4"
if [[ ! $numString1 =~ ^1.7 ]]; then
echo "String \"$numString1\" does not start with a \"1\", followed by any character, and followed by a 7. "
echo "This string does not match the regex: ^1.7"
fi

echo "Example 5"
if [[ $numString2 =~ 9$ ]]; then
echo "String \"$numString2\" ends with a \"9\", and matches the regex: 9$"
fi