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

后端 未结 4 788
半阙折子戏
半阙折子戏 2021-01-16 00:39

To see what file to invoke the unrar command on, one needs to determine which file is the first in the file set.

Here are some sample file names, of which - naturall

4条回答
  •  抹茶落季
    2021-01-16 00:57

    The short answer is that it's not possible to construct a single regex to satisfy your problem. Ruby 1.8 does not have lookaround assertions (the (?

    1) Use more than one regex to do it.

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

    2) Use the regex engine for ruby 1.9, Oniguruma. It supports lookaround assertions, and you can install it as a gem for ruby 1.8. After that, you can do something like this:

    def is_first_rar(filename)
        reg = Oniguruma::ORegexp.new('.*(?:(?

提交回复
热议问题