Search code examples
regexbashrar

regex rar archive


I have this regex:

.*part0?0?1.rar

and this:

match

S01.rar
S23.part1.rar
s4.part01.rar
Movie.rar
Youtube Movie.part1.rar
123Mov Ddaa.part01.rar
123Mov Ddaa.part001.rar

dont match

S01.part02.rar
S2.part4.rar
Movie.part3.rar
Youtube Movie.part08.rar
123Mov Ddaa.part002.rar

i test with https://regexr.com/

How can i all the words under match match? Sry for my very very bad english :/


Solution

  • It turns out you want to use a regex in Bash. The regex you need will match any string containing part not followed with a number other than 1, 01 or 001, i.e. the number after part must be greater than 1 and negate the result.

    See Bash demo printing anything:

    file="Youtube Movie.part01.rar"
    regex='part(0*[2-9][0-9]*|0*1[0-9]+)\.rar'
    if ! [[ $file =~ $regex ]]
    then
        echo "anything"
    fi
    

    Here, part(0*[2-9][0-9]*|0*1[0-9]+)\.rar matches

    • part - a literal substring part
    • (0*[2-9][0-9]*|0*1[0-9]+) - either of the 2 alternatives:
      • 0* - zero or more 0 chars
      • [2-9] - a digit from 2 to 9
      • [0-9]* - any 0+ ASCII digits
      • | or
      • 0*1[0-9]+ - zero or more 0 chars, 1 and then one or more any digits
    • \. - a dot
    • rar - a rar substring.

    The if ! [[ $file =~ $regex ]] means "if the $regex does not match the $file, show anything".