如何使用正则表达式只返回文件名的一部分?

时间:2012-07-18 22:15:11

标签: regex sed grep expression

我无法创建只返回字符串一部分的正则表达式。

传递以下字符串:

/path/of/the/file/1 - 2 - Lecture 1.2_ Max Bense (13_49).mp4
/path/of/the/file/1 - 3 - Lecture 1.3_ Michael Friedman (12_15).mp4
/path/of/the/file/2 - 1 - Lecture 2.1_ Paul Feyerabend (12_55).mp4
/path/of/the/file/2 - 2 - Lecture 2.2_ Alhazen (11_37).mp4
/path/of/the/file/3 - 2 - Lecture 3.2_ Study Case - Dominicus Gundissalinus (14_30).mp4 
/path/of/the/file/3 - 3 - Lecture 3.3_ Study Case - Carl Friedrich von Weizsacker (11_48).mp4

它应该分别只返回以下部分:

Max Bense
Michael Friedman
Paul Feyerabend
Alhazen
Study Case - Dominicus Gundissalinus
Study Case - Carl Friedrich von Weizsacker

3 个答案:

答案 0 :(得分:0)

awk似乎很容易。它使用字符_(拆分字段中的行,因此名称将是第二个,然后删除该字段的前导和尾随空格:

awk '
    BEGIN { 
        FS = "[_(]" ;
    } 
    { 
        gsub( /^ *| *$/, "", $2 ); 
        print $2 ;
    }
' infile

输出:

Max Bense
Michael Friedman
Paul Feyerabend
Alhazen
Study Case - Dominicus Gundissalinus
Study Case - Carl Friedrich von Weizsacker

答案 1 :(得分:0)

使用PCRE和Positive Lookbehind

如果您可以访问支持PCRE表达式的正则表达式引擎,则可以使用正向lookbehind从MP3列表中获取所需的文本。例如:

pcregrep -o '(?<=_ )([^(]+)' /tmp/foo

使用Sed

如果你没有Perl兼容的grep,那么你可以使用sed代替。它的可读性差得多,但便携性更强。例如:

sed 's/.*_ \([^(]\+\).*/\1/' /tmp/foo

答案 2 :(得分:0)

这是一个JavaScript解决方案:

var files=["/path/of/the/file/1 - 2 - Lecture 1.2_ Max Bense (13_49).mp4",
"/path/of/the/file/1 - 3 - Lecture 1.3_ Michael Friedman (12_15).mp4",
"/path/of/the/file/2 - 1 - Lecture 2.1_ Paul Feyerabend (12_55).mp4",
"/path/of/the/file/2 - 2 - Lecture 2.2_ Alhazen (11_37).mp4",
"/path/of/the/file/3 - 2 - Lecture 3.2_ Study Case - Dominicus Gundissalinus (14_30).mp4",
"/path/of/the/file/3 - 3 - Lecture 3.3_ Study Case - Carl Friedrich von Weizsacker (11_48).mp4​​​​"];
var regex=/_\s(.+)\s/;

​for (var i = 0; i < files.length; i++) {
    console.log(regex.exec(files[i])[1]);
}​

http://jsfiddle.net/g8zPv/

相关问题