perl regex匹配以pattern开头的字符串

时间:2015-05-05 16:33:41

标签: regex perl

我有以下perl代码。如果我的B有字符串<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /manage/ RewriteRule ^([a-zA-Z0-9-]+)/?$ /manage/index.php?p=$1 [QSA,NC,L] RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /manage/index.php?p=$1&id=$2 [QSA,NC,L] </IfModule> ,我正在尝试将flag设置为1,这正常。但是现在我想将$location设置为/foo/HELLO,如果flag0(它有$location以及 之后的任何内容,除了HELLO

如何在/foo/*条件下构建我的正则表达式来实现它?

/foo/

3 个答案:

答案 0 :(得分:2)

您在<footer>,因此您已经知道它没有<html> <head></head> <body> <nav><!--Sectioning element for navigation--> <header> <h1>Navigate simply around the site...</h1> <p>and learn more</p> </header><!--All flow content are considered a part of my header inside the nav section --> <ul> <li>item 1</li> <li>item 2</li> </ul> </nav> <article> <section> <header> <h1>Main Article Title</h1> <cite>A sub message</cite><!--Again all content is considered an header of the section--> </header> <p>Some content</p> </section> </article> </body> </html> 。因此,只需验证else是否仍然存在:

/foo/HELLO

答案 1 :(得分:0)

您可以使用grepmap构建字符串的哈希值以及相关的标记值:

use strict;
use warnings;

use Data::Dumper qw(Dumper);

my @locations= (
  "/path/with/foo/HELLO",
  "/path/with/foo/def-abc-addons.install",
  "/path/with/foo/def-abc-addons.lintian-overrides",
  "/path/with/foo/def-abc-addons.postrm",
  "/abc/def/ggfg",
  "/frgrg/hjytj/dgth",
);

my %h = map { $_ =~ m|/foo/HELLO| ? ($_ => 1) : ($_=>0)  } grep {m|/foo/\w+|} @locations;

print Dumper \%h;

现在%h

    {
      '/path/with/foo/def-abc-addons.install' => 0,
      '/path/with/foo/def-abc-addons.lintian-overrides' => 0,
      '/path/with/foo/def-abc-addons.postrm' => 0,
      '/path/with/foo/HELLO' => 1
    };

只需更改正则表达式以适应您要完成的任务。

答案 2 :(得分:0)

认为您正在寻找的是过滤那些路径中/foo/但不是/foo/HELLO/的文件。您可以使用grep运算符执行此操作,如下所示

如果您的要求比您描述的要严格,则此代码需要更多工作。这里有许多边缘案例和陷阱,并且没有一个被解决。例如,如果路径包含/foo/HELLONWHEELS,那么它将被拒绝,这可能是你的意图,也可能不是你的意图

use strict;
use warnings;
use 5.010;

my @locations= qw(
  /path/with/foo/HELLO
  /path/with/foo/def-abc-addons.install
  /path/with/foo/def-abc-addons.lintian-overrides
  /path/with/foo/def-abc-addons.postrm
  /abc/def/ggfg
  /frgrg/hjytj/dgth
);

say for grep m{ /foo/ (?!HELLO) }x, @locations;

<强>输出

/path/with/foo/def-abc-addons.install
/path/with/foo/def-abc-addons.lintian-overrides
/path/with/foo/def-abc-addons.postrm