perl - 使用perl从文本中提取段落

标签 perl

我想从从数据库中检索到的文本变量中提取段落。

为了从文件处理程序中提取 pargaphs,我使用以下代码:

local $/ = undef;
@paragarphs =<STDIN> 

使用 perl 从文本变量中提取段落的最佳选择是什么?cpan 上是否有执行此类任务的模块?

最佳答案

你快到了。将 $/ 设置为 undef 将一次性读入整个文本。

你想要的是 local $/= ""; 启用段落模式,根据 perldoc perlvar (强调我自己):

$/

The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like awk's RS variable, including treating empty lines as a terminator if set to the null string (an empty line cannot contain any spaces or tabs). You may set it to a multi-character string to match a multi-character terminator, or to undef to read through the end of file. Setting it to "\n\n" means something slightly different than setting to "" , if the file contains consecutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line. Setting to "\n\n" will blindly assume that the next input character belongs to the next paragraph, even if it's a newline.


当然,可以从字符串而不是文件中获取文件句柄:

use strict;
use warnings;
use autodie;

my $text = <<TEXT;
This is a paragraph.

Here's another one that 
spans over multiple lines.

Last paragraph
TEXT

local $/ = "";
open my $fh, '<', \$text;

while ( <$fh> ) {

    print "New Paragraph: $_";
}

close $fh;

输出

New Paragraph: This is a paragraph.

New Paragraph: Here's another one that
spans over multiple lines.

New Paragraph: Last paragraph

关于perl - 使用perl从文本中提取段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12561822/

相关文章:

python - 如何从 Python 使用 Perl 库?

perl - 将标量和散列传递给 Perl 中的子程序

Perl 谷歌日历 API

xml - XPath 表达式中的 Perl 变量

php - 是否可以用 PHP 编写 WHM 插件?

mysql - 当记录以下划线开头时出现重复条目​​错误

perl - 在散列 Perl 中增加值

string - 从 perl 变量中删除一个空格

perl - 为什么我不应该在 Perl 代码中使用 shell 工具?

perl - 在 perl 中散列的数组