# Regular Expression Matching (REMATCH)



http://molk.ch/tips/gnu/bash/rematch.html



```bash
#!/usr/bin/env bash

FILE="/Users/bruno/Downloads/Better.Call.Saul.S06E07.FRENCH.LD.AMZN.WEB-DL.x264-FRATERNiTY.mkv"

# Extension video file
DEST_EXT=mp4

filename=$(basename "$FILE")
filename=${filename%.*}

# Series: SxxEyy
REGEX="([sS]([0-9]{2,}|[X]{2,})[eE]([0-9]{2,}|[Y]{2,}))"


if [[ $filename =~ $REGEX ]]; then
    MATCH0="${BASH_REMATCH[0]}"
	echo "$MATCH0"				# S06E07

    MATCH1="${BASH_REMATCH[1]}"
	echo "$MATCH1"				# S06E07
	
    MATCH2="${BASH_REMATCH[2]}"
	echo "$MATCH2"				# 06
	
    MATCH3="${BASH_REMATCH[3]}"
	echo "$MATCH3"				# 07
					
    e=$(echo ${filename%$MATCH0*} | sed 's/\./\ /g' | xargs)
    echo "$e"					# Better Call Saul
    					
    new_name="$e - $MATCH0.$DEST_EXT"
    echo "$new_name"			# Better Call Saul - S06E07.
    
else
	echo "no match"
fi
```

