'Regex that matches comma separated list of 4 strings in bash

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.



Solution 1:[1]

It is quite straightforward with awk:

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

Solution 2:[2]

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$" 

Solution 3:[3]

Nothing in the question restricts "things" to be alphanumeric. Use this grep command and regular expression:

grep -E "([^,]*),([^,]*),\1,\2".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fravadona
Solution 2 Barmar
Solution 3 Paul Olsen