简单的 Grep 不匹配问题

发布于 2024-11-19 11:52:29 字数 974 浏览 7 评论 0原文

我正在使用 Ubuntu 10.10 并使用 Grep 来处理一些 HTML 文件。

以下是 HTML 片段:

<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">

我想提取 one-hd.movanother-hd.mov 但忽略 normal.mov

这是我的代码:

example='<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">'
echo $example | grep -Po '(?<=video.php\?video=).*?(?=-hd.mov">)'

结果是:

one
normal.mov"><img src="/2.jpg"><a href="video.php?video=another

但我想要

one
another

那里不匹配。

这是因为所谓的贪婪正则表达式吗?

我正在唱 GREP,但欢迎使用任何命令行 bash 工具来解决这个问题,例如 sed 等。

非常感谢。

I am using Ubuntu 10.10 and using Grep to process some HTML files.

Here is the HTML snippet:

<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">

I would like to extract one-hd.mov and another-hd.mov but ignore normal.mov.

Here is my code:

example='<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">'
echo $example | grep -Po '(?<=video.php\?video=).*?(?=-hd.mov">)'

The result is:

one
normal.mov"><img src="/2.jpg"><a href="video.php?video=another

But I want

one
another

There is a mismatch there.

Is this because of the so-called Greedy Regular Expression?

I am sing GREP but any command line bash tools are welcome to solve this problem like sed etc.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-11-26 11:52:29

您想使用 Perl 正则表达式进行 grep - 为什么不直接使用 perl?

echo "$example" | perl -nle 'm/.*?video.php\?video=([^"]+)">.*video.php\?video=([^"]+)".*/; print "=$1=$2="'

将打印

=one-hd.mov=another-hd.mov=

You want use Perl regexes for grep - why not directly perl?

echo "$example" | perl -nle 'm/.*?video.php\?video=([^"]+)">.*video.php\?video=([^"]+)".*/; print "=$1=$2="'

will print

=one-hd.mov=another-hd.mov=
云醉月微眠 2024-11-26 11:52:29

这是使用 xmlstarlet 的解决方案:

$ example='<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">'
$ echo $example | xmlstarlet fo -R 2>/dev/null | xmlstarlet sel -t -m "//*[substring(@href, string-length(@href) - 6, 7) = '-hd.mov']" -v 'substring(@href,17, string-length(@href) - 17 - 3)' -n
one-hd
another-hd

$

Here is a solution using xmlstarlet:

$ example='<a href="video.php?video=one-hd.mov"><img src="/1.jpg"><a href="video.php?video=normal.mov"><img src="/2.jpg"><a href="video.php?video=another-hd.mov">'
$ echo $example | xmlstarlet fo -R 2>/dev/null | xmlstarlet sel -t -m "//*[substring(@href, string-length(@href) - 6, 7) = '-hd.mov']" -v 'substring(@href,17, string-length(@href) - 17 - 3)' -n
one-hd
another-hd

$
花伊自在美 2024-11-26 11:52:29

使用 awk 的解决方案:

{
    for(i=1;i<NF;i++) {
        if ($i ~ /mov/) {
            if ($i !~ /normal/){
                sub(/^.*=/, "", $i)
                print $i
            }
        }
    }
}

输出:

$ awk -F'"' -f h.awk html
one-hd.mov
another-hd.mov

但我强烈建议您使用 html 解析器来代替,例如 BeautifulSoup

Solution using awk:

{
    for(i=1;i<NF;i++) {
        if ($i ~ /mov/) {
            if ($i !~ /normal/){
                sub(/^.*=/, "", $i)
                print $i
            }
        }
    }
}

outputs:

$ awk -F'"' -f h.awk html
one-hd.mov
another-hd.mov

But I strongly advice you to use a html-parser for this instead, something like BeautifulSoup

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文