Issue
I am writing a bash script where I need to pass a parameter that follows a simple pattern: string:string
.
- Colon must not be the first or last character of the string
- There must only be exactly one colon in the string
I found this question but it's looking for the colon at the start of the string. I need the colon to be in the middle of the string and that there's at most one colon in the string and nothing more. How do I make this check preferrably using regex?
Solution
To match any character except ":", use [^:]+ in regex. This regex expression should be able to match one and only one colon.
if [[ $var =~ ^[^:]+:[^:]+$ ]]; then echo "match"; fi
Answered By - Jesse C Answer Checked By - Senaida (WPSolving Volunteer)