을 quotemeta를 사용하는 또 다른 방법이다 :
use strict;
use warnings;
my @dirs = qw(
/root/demo
/etc/demo
);
# Sorted by length descending in case there are subdirs.
my $list_dirs = join '|', map {quotemeta} sort { length($b) <=> length($a) } @dirs;
while (<DATA>) {
chomp;
if (my ($subdir) = m{^(?:$list_dirs)(/.*)}) {
print "$subdir\n";
}
}
__DATA__
/root/demo/home/test/sample/someroot.txt
/etc/demo/home/test/sample/someetc.txt
출력을 :
다음은 보여줍니다.
펄 샘플 :
use strict;
use warnings;
my @defaults = ('/root/demo/', '/etc/demo/');
$/ = undef;
my $testdata = <DATA>;
my $regex = '(?:' . join('|', map(quotemeta($_), @defaults)) . ')(\S*)';
print $regex, "\n\n";
while ($testdata =~ /$regex/g)
{
print "Found /$1\n";
}
__DATA__
/root/demo/home/test/sample/somefile.txt
/etc/demo/home/test/sample/somefile.txt
출력 :.?.?
(?:\/root\/demo\/|\/etc\/demo\/)(\S*)
Found /home/test/sample/somefile.txt
Found /home/test/sample/somefile.txt
출처
2014-11-02 18:46:22
sln
또는'\/(? 루트 | 등) \/데모 \의 K * \ \ + w (= \의 | $)' –
고마워,하지만 일반 정규식을 갖고 싶다.이 정규식은/root/demo/또는/etc/demo /를 확인한다. 내 기본 디렉토리는/etc/demo/a/b/또는/root/demo/a/b /와 같이 n 레벨 디렉토리가 될 수 있습니다. 기본 디렉토리를 전달하는 Bascially는 n 레벨의 디렉토리로 구성 가능합니다. – Bablu
예상 출력과 함께 더 많은 예제를 게시 할 수 있습니까? –