【问题标题】:How do I extract quoted portions from a text in perl?如何从 perl 中的文本中提取引用部分?
【发布时间】:2021-01-19 01:14:30
【问题描述】:

例如,来自这样的文本:

到 1984 年,迪伦开始远离“重生”的标签。他告诉滚石杂志的库尔特洛德:“我从来没有说过我重生了。这只是一个媒体术语。我不认为我是不可知论者。我一直认为有一种优越的力量,那就是这不是真实的世界,还有未来的世界。”

我要提取:

  • “重生”
  • “我从来没有说过我重生了。这只是一个媒体术语。我不认为我是不可知论者。我一直认为有一种优越的力量,这不是现实世界还有一个世界即将到来。”

文本本身显然没有固定数量的引号,因此解决方案需要提取所有引用部分。

我正在尝试像这样使用Text::Balanced

extract_delimited($text, "\""); 

在一个循环中,但我什至无法提取“重生”——这将是一个好的开始。

Text::Balanced 是正确的工具吗?我哪里错了?

【问题讨论】:

  • 文档清楚地说明提取initial子字符串(强调我的)。
  • 我建议Text::Balanced,是的。但是您需要仔细阅读文档,它不是正确使用的最简单工具。我可以轻松找到一些示例:thisthisthis。还有更多...

标签: perl


【解决方案1】:

如果您不需要处理引号内的引号之类的东西,Text::Balanced 可能会过大。

假设" 字符在字符串的开头或前面有一个空格将打开一个引号,而下一个" 在字符串的末尾,或者后面有一个非单词字符它将结束引号,然后/(?:\s|\A)(\".+?\")(?:\W|\z)/sm 应该捕获带引号的字符串,包括引号。

添加 /g 修饰符以捕获所有引号,您会得到:

use strict;
use warnings;
use Data::Dumper;

my $data = <<'DATA';
By 1984, Dylan was distancing himself from the "born again" label. He told
Kurt Loder of Rolling Stone magazine: "I've never said I'm born again.
That's just a media term. I don't think I've been an agnostic. I've always
thought there's a superior power, that this is not the real world and that
there's a world to come."
DATA

my @quoted_parts = ( $data =~ /(?:\s|\A)(\".+?\")(?:\W|\z)/gsm );

print Dumper \@quoted_parts;

Text::Balanced 在您需要处理例如可能嵌套的不同括号时很有用,例如 "( [ ( ) ] )" 并且您需要确保正确的结束括号与正确的结束括号匹配起始括号。当您希望引号能够包含转义的引号字符时,它很有用。之类的东西。它实际上是用于处理 XML、JSON、编程语言、配置文件等行的形式语言解析。不适用于解析自然语言。

【讨论】:

    【解决方案2】:

    只是因为你尝试了Text::Balanced 没有成功——也许你想要

    #!/usr/bin/env perl
    
    use Data::Dumper;
    use Params::Validate qw(:all);
    use Text::Balanced qw(extract_delimited extract_multiple);
    use 5.01800;
    use warnings;
    
        sub dump_stringsQuoted { # Dumps quoted strings
            my ($text_S)=validate_pos(@_,{ type=>SCALAR });
            warn Data::Dumper->Dump([\$text_S],[qw(*text)]),' ';;
    
            for (extract_multiple($text_S, [sub {extract_delimited($_[0],q{"})}], undef, 1)) {
                say $_;
                };
             }; # dump_stringsQuoted:
    
    local $/;
    dump_stringsQuoted(<DATA>);
    __DATA__
    By 1984, Dylan was distancing himself from the "born again" label. He told Kurt
    Loder of Rolling Stone magazine: "I've never said I'm born again. That's just a
    media term. I don't think I've been an agnostic. I've always thought there's a
    superior power, that this is not the real world and that there's a world to come."
    

    产生

    duh >perl TB.pl
    $text = \'By 1984, Dylan was distancing himself from the "born again" label. He told Kurt
    Loder of Rolling Stone magazine: "I\'ve never said I\'m born again. That\'s just a
    media term. I don\'t think I\'ve been an agnostic. I\'ve always thought there\'s a
    superior power, that this is not the real world and that there\'s a world to come."';
      at TB.pl line 11, <DATA> chunk 1.
    "born again"
    "I've never said I'm born again. That's just a
    media term. I don't think I've been an agnostic. I've always thought there's a
    superior power, that this is not the real world and that there's a world to come."
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多