Home > front end >  Regex that matches comma separated list of 4 strings in bash
Regex that matches comma separated list of 4 strings in bash

Time:04-02

Im trying to write a regex function in bash where the first and third thing in the list are the same and the second and fourth thing in the list are the same.

grep -E "^(([0-9a-zA-Z][0-9a-zA-Z_]*)([,][0-9a-zA-Z][0-9a-zA-Z_]*)*)$" 

This is my regular expression as of now. I am unsure on how to check if the first and third, second and fourth are the same. Say for example a,b,a,b matches but a,b,b,b doesnt.

CodePudding user response:

It is quite straightforward with awk:

awk -F, '$1 == $3 && $2 == $4'

CodePudding user response:

Use a back-reference to check that one part matches a previously captured string.

grep -E -i "^([0-9a-z][0-9a-z_]*),([0-9a-z][0-9a-z_]*),\1,\2$" 

CodePudding user response:

With GNU sed:

echo 'a,b,a,b' | sed -E '/(.*),(.*),\1,\2/!d'

Or only which bash:

x='a,b,a,b'
[[ $x =~ (.*),(.*),(.*),(.*) ]] &&
[[ ${BASH_REMATCH[1]} == ${BASH_REMATCH[3]} &&
   ${BASH_REMATCH[2]} == ${BASH_REMATCH[4]} ]] &&
echo "okay"

or without regex:

IFS=, read -r a b c d <<<"a,b,a,b"; [[ $a == $c && $b == $d ]] && echo "okay"

Output:

a,b,a,b
  • Related