简体   繁体   English

Perl:帮助编写正则表达式

[英]Perl: Help writing a regular expression

I am trying to write a common regular expression for the below 3 cases: 我正在尝试为以下3种情况编写一个通用的正则表达式:

  • Supernatural_S07E23_720p_HDTV_X264-DIMENSION.mkv 邪恶力量_S07E23_720p_HDTV_X264-DIMENSION.mkv
  • the.listener.313.480p.hdtv.x264-2hd.mkv 听众313.480p.hdtv.x264-2hd.mkv
  • How.I.met.your.mother.s02e07.hdtv.x264-xor.avi 我如何与您的母亲相伴s02e07.hdtv.x264-xor.avi

Now my regular exoression should remove the series name from the original string i,e the output of above string will be: 现在我的例行表达应从原始字符串中删除系列名称,即上述字符串的输出为:

  • S07E23_720p_HDTV_X264-DIMENSION.mkv S07E23_720p_HDTV_X264-DIMENSION.mkv
  • 313.480p.hdtv.x264-2hd.mkv 313.480p.hdtv.x264-2hd.mkv
  • s02e07.hdtv.x264-xor.avi s02e07.hdtv.x264-xor.avi

Now for the basic case of supernatural string I wrote the below regex and it worked fine but as soon as the series name got multiple words it fails. 现在,对于supernatural string的基本情况,我编写了以下正则表达式,它可以正常工作,但是一旦系列名称得到多个单词,它就会失败。

$string =~ s/^(.*?)[\.\_\- ]//i; #delimiter can be (. - _ )

So, I have no idea how to proceed for the aboves cases I was thinking along the lines of \\w+{1,6} but it also failed to do the required. 因此,我不知道如何针对\\w+{1,6}上述情况继续进行处理,但是它也无法满足要求。

PS: Explanation of what the regular expression is doing will be appreciated. PS:对正则表达式在做什么的解释将不胜感激。

you can detect if the .'s next token contains digit, if not, consider it as part of the name. 您可以检测。的下一个标记是否包含数字,如果没有,则将其视为名称的一部分。

HOWEVER, I personally think there is no perfect solution for this. 但是,我个人认为没有完美的解决方案。 it'd still meet problem for something like: 它仍然会遇到类似以下问题:

24.313.480p.hdtv.x264-2hd.mkv            // 24
Warehouse.13.s02e07.hdtv.x264-xor.avi    // warehouse 13

As StanleyZ said, you'll always get into trouble with names containing numbers. 正如StanleyZ所说的那样,您总是会遇到包含数字的名称的麻烦。

But, if you take these special cases appart, you can try : 但是,如果您采取这些特殊情况,可以尝试:

#perl

$\=$/;

map {

    if (/^([\w\.]+)[\.\_]([SE\d]+[\.\_].*)$/i) {
        print "Match : Name='$1'        Suffix='$2'";
    } else {
        print "Did not match $_";
    }
}
qw!
    Supernatural_S07E23_720p_HDTV_X264-DIMENSION.mkv
    the.listener.313.480p.hdtv.x264-2hd.mkv
    How.I.met.your.mother.s02e07.hdtv.x264-xor.avi
  !;

which outputs : 输出:

Match : Name='Supernatural'     Suffix='S07E23_720p_HDTV_X264-DIMENSION.mkv'
Match : Name='the.listener'     Suffix='313.480p.hdtv.x264-2hd.mkv'
Match : Name='How.I.met.your.mother'     Suffix='s02e07.hdtv.x264-xor.avi'

note : aren't you doing something illegal ? 注意:您不是在做违法的事吗? ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM