Regular expression to match only the first file in a set of RAR files

To find out which file to invoke the unrar command is included, you need to determine which file is the first in the file set.

Here are a few examples of file names, of which - naturally - only the first group should be mapped:

yes.rar
yes.part1.rar
yes.part01.rar
yes.part001.rar

no.part2.rar
no.part02.rar
no.part002.rar
no.part011.rar

One (limited) way to do this is with PCRE compatible regular expressions:

.*(?:(?<!part\d\d\d|part\d\d|\d)\.rar|\.part0*1\.rar)

This did not work in Ruby when I tested it with Rejax .

How would you write a single Ruby compatible regular expression to match only the first file in the RAR file set?

+1
source share
4 answers

, . Ruby 1.8 lookaround ((? <! stuff regex), . .

1) .

def is_first_rar(filename)
    if ((filename =~ /part(\d+)\.rar$/) == nil)
        return (filename =~ /\.rar$/) != nil
    else
        return $1.to_i == 1
    end
end

2) regex engine ruby ​​1.9, Oniguruma. , ruby ​​1.8. - :

def is_first_rar(filename)
    reg = Oniguruma::ORegexp.new('.*(?:(?<!part\d\d\d|part\d\d|\d)\.rar|\.part0*1\.rar)')
    match = reg.match(filename)
    return match != nil
end
+3

, , . , .

RAR , , , RAR.

HEAD_FLAGS -:
2

0x0100 - ( RAR 3.0 )

, RAR, , , . , . RAR-, .

, .

+4

,

^(yes|no)\.(rar|part0*1\.rar)$

"yes | no" . , , , , "yes | no" .

UPDATE: . , , ...

0

() (, , , ). , , if s?

0

Source: https://habr.com/ru/post/1731674/


All Articles