【问题标题】:Using Regex to search for a string that contains "http://" and does not contain "mysite.com"使用 Regex 搜索包含“http://”且不包含“mysite.com”的字符串
【发布时间】:2012-03-14 13:02:25
【问题描述】:

如何编写 Regular Rxpression 来搜索包含“http://”且不包含“mysite.com”的字符串?

【问题讨论】:

  • 这样的事情可能会起作用:^(http://)((?!mysite\.com).)*$
  • 这将匹配http://www.mysite.com
  • @M42:不,它没有。我在这里测试过regexlib.com/RETester.aspx,YMMV。
  • 不!请重新阅读我的问题我想要“http://”但不是“mysite.com”......用简单的英语我想获取所有外部链接
  • 试图将正则表达式转换成布尔逻辑,最好用适当的编程语言完成,这是一项吃力不讨好的工作。虽然可以使用复杂的前瞻来编写/PAT1/ and not /PAT2/,使其只是一种模式,但这是一项痛苦的任务。 你不想这样做。使用像vi这样的真实编辑器,你会简单地进行确认检查,这样你就可以手动干预::%s/foo/bar/gc

标签: html regex parsing dreamweaver


【解决方案1】:

警告

试图用正确的编程语言将正则表达式绑定到布尔逻辑中是一项吃力不讨好的工作。虽然可以使用复杂的前瞻来编写/PAT1/ and not /PAT2/,使其只是一种模式,但这是一项痛苦的任务。 你不要这样做!

你应该首先解释你真正在做什么——文本编辑器中的某种匹配操作。你没有。因此,您会得到一个一般性的答案,这将很难适应您的本地化情况。

快速解答

(?sx)                 # let dot cross newlines, enable comments & whitspace
(?= .* http://     )  # lookahead assertion for http://
(?! .* mysite\.com )  # lookahead negation  for mysite.com

使用 Perl 语法,您可以通过这种方式将该(预)编译的模式粘贴到变量中以供将来使用:

my $is_valid_rx = qr{
    (?= .* http://     )  # lookahead assertion for http://
    (?! .* mysite\.com )  # lookahead negation  for mysite.com
}sx;                      # /s to cross newlines, /x for comments & whitespace

# then later on…
if ($some_string =~ $is_valid_rx) { 
     # your string has an http blah and lacks a mysite blah
}

但是,如果您的目标是提取所有此类链接,那将无济于事,因为这些前瞻不会告诉您链接出现在字符串中的哪个位置。

在这种情况下,编写一些东西来提取链接然后过滤掉不需要的情况会容易得多,使用两个单独的正则表达式而不是尝试做所有事情。

 @all_links = ($some_string =~ m{ https?://\S+ }xg);
 @good_links = grep !/mysite\.com/, @all_links;

请注意,不会尝试仅匹配包含有效 URL 字符的链接,也不会像纯文本中经常出现的意外尾随标点符号。

现在,来看看真正的答案

还请注意,如果您使用它来解析 HTML,那么上面概述的方法只是一种快速而简单、快速且松散的链接提取方式。构建产生大量误报的有效输入很容易,而构建产生误报的输入也不难。

相比之下,这里是一个完整的程序,它会在其 URL 参数中转储所有 <a ...><img ...> 链接地址,并且实际上这样做是正确的,因为它使用了真正的解析器。

#!/usr/bin/env perl
#
# fetchlinks - fetch all <a> and <img> links from listed URL args
# Tom Christiansen <tchrist@perl.com>
# Wed Mar 14 08:03:53 MDT 2012
#
use strict;
use warnings;

use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

die "usage: $0 url ...\n" unless @ARGV;

for my $arg (@ARGV) {
    my @links = fetch_wanted_links($arg => qw<a img>);
    for my $link (@links) {
        print "$arg => " if @ARGV > 1;
        print "$link\n";
    }
}

exit;

sub fetch_wanted_links {
    my($url, @wanted) = @_;

    my %wanted;
    @wanted{@wanted} = (1) x @wanted;

    my $agent = LWP::UserAgent->new;

    # Set up a callback that collect links of the wanted variety
    my @hits = ();

    # Make the parser.  Unfortunately, we don't know the base yet
    # (it might be different from $url)
    my $parser = new HTML::LinkExtor sub {
       my($tag, %attr) = @_;
       return if %wanted and not $wanted{$tag};
       push @hits, values %attr;
    };

    # Request document and parse it as it arrives
    my $response = $agent->request(
           HTTP::Request->new(GET => $url),
           sub { $parser->parse( $_[0] ) },
    );

    # Expand all image URLs to absolute ones
    my $base = $response->base;
    @hits = map { $_ = url($_, $base)->abs } @hits;
    return @hits;
}

如果您在这样的 URL 上运行它,它会给出所有锚点和图像链接的统计信息:

$ perl fetchlinks http://www.perl.org/
http://www.perl.org/
http://st.pimg.net/perlweb/images/camel_head.v25e738a.png
http://www.perl.org/
http://www.perl.org/learn.html
http://www.perl.org/docs.html
http://www.perl.org/cpan.html
http://www.perl.org/community.html
http://www.perl.org/contribute.html
http://www.perl.org/about.html
http://www.perl.org/get.html
http://www.perl.org/get.html
http://www.perl.org/get.html
http://www.perl.org/about.html
http://www.perl.org/learn.html
http://st.pimg.net/perlweb/images/icons/learn.v0e1f83c.png
http://www.perl.org/learn.html
http://www.perl.org/community.html
http://st.pimg.net/perlweb/images/icons/community.v03bf8ce.png
http://www.perl.org/community.html
http://www.perl.org/docs.html
http://st.pimg.net/perlweb/images/icons/docs.v2622a01.png
http://www.perl.org/docs.html
http://www.perl.org/contribute.html
http://st.pimg.net/perlweb/images/icons/cog.v08b9acc.png
http://www.perl.org/contribute.html
http://www.perl.org/dev.html
http://www.perl.org/contribute.html
http://www.perl.org/cpan.html
http://st.pimg.net/perlweb/images/icons/cpan.vdc5be93.png
http://www.perl.org/cpan.html
http://www.perl.org/events.html
http://st.pimg.net/perlweb/images/icons/cal.v705acef.png
http://www.perl.org/events.html
http://www.perl6.org/
http://st.pimg.net/perlweb/images/icons/perl6.v8ff6c63.png
http://www.perl6.org/
http://www.perl.org/dev.html
http://www.perlfoundation.org/
http://st.pimg.net/perlweb/images/icons/onion.vee5cb98.png
http://www.perlfoundation.org/
http://www.cpan.org/
http://search.cpan.org/~jtang/Net-Stomp-0.45/
http://search.cpan.org/~vaxman/Array-APX-0.3/
http://search.cpan.org/~salva/Net-SFTP-Foreign-1.71/
http://search.cpan.org/~grandpa/Win32-MSI-HighLevel-1.0008/
http://search.cpan.org/~teejay/Catalyst-TraitFor-Component-ConfigPerSite-0.06/
http://search.cpan.org/~jwieland/WebService-Embedly-0.04/
http://search.cpan.org/~mariab/WWW-TMDB-API0.04/
http://search.cpan.org/~teejay/SOAP-Data-Builder-1/
http://search.cpan.org/~dylan/WWW-Google-Translate-0.03/
http://search.cpan.org/~jtbraun/Parse-RecDescent-1.967_008/
http://www.perl.org/get.html
http://www.perl.org/learn.html
http://www.perl.org/docs.html
http://www.perl.org/community.html
http://www.perl.org/events.html
http://www.perl.org/siteinfo.html#sponsors
http://www.yellowbot.com/
http://st.pimg.net/perlweb/images/friends/yellowbot.vcc29f5b.gif
http://www.perl.org/
http://blogs.perl.org/
http://jobs.perl.org/
http://learn.perl.org/
http://dev.perl.org/
http://creativecommons.org/licenses/by-nc-nd/3.0/us/
http://i.creativecommons.org/l/by-nc-nd/3.0/us/80x15.png
http://www.perl.org/siteinfo.html

对于任何比在文件上运行快速grep 以查看一般结果更重要的工作,您需要使用适当的解析器来执行此类操作。

【讨论】:

  • 别忘了在mysite.com中转义.
  • 我不知道thisismysite.com是否必须匹配,但是在mysite之前添加一个字边界是安全的。
  • @Qtax 事实证明,不仅如此。谢谢。
  • @M42 这是一个有趣的——我认为,开放的——问题。这取决于最初的询问者真正需要什么。
  • @M42,在这种情况下,还需要进行一些检查,thisismysite.computer
【解决方案2】:

试试/http:\/\/(?!mysite.com)/(perl 风格,也可以在 javascript 中使用)

【讨论】:

  • 我正在使用 Adob​​e Dreamwaver 编辑器搜索,它使我能够使用正则表达式进行搜索。
  • 不!请重新阅读我的问题我想要“http://”但不是“mysite.com”......用简单的英语我想获取所有外部链接。
  • 它不会匹配 http://www.mysite.com 因为 (! 构造。但我在 jsfiddle 上试了一下,它匹配 http://plop.mysite.com 例如jsfiddle.net/nicocube/h3QxS
猜你喜欢
  • 2013-08-14
  • 2019-05-17
  • 2013-06-26
  • 2013-11-10
  • 1970-01-01
  • 2021-06-12
  • 2020-05-01
  • 2012-05-07
  • 2019-07-08
相关资源
最近更新 更多