简体   繁体   English

使用RegEp在Perl上打印文本

[英]Printing Text on Perl with RegEp

I want to extract the strings from my source text which meet these conditions: 我想从符合以下条件的源文本中提取字符串:

  • The first letter of the string is both the first letter on a line and a vowel. 字符串的首字母既是行中的首字母又是元音。
  • The string continues to the next . 字符串继续到下一个. or ; ; (regardless of line breaks) (不管换行)

I tried this: 我尝试了这个:

$_ = join("",<>);

foreach ($text =~ /^[AEIOUaeiou][\w\s,]*[.;]/m)
{
    print "$text\n";
}

An example of how this should work: 有关其工作方式的示例:

Input:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In aliquet lobortis vehicula. Sed
augue lectus, mattis nec tempus vel, mattis vel sapien, morbi venenatis faucibus pulvinar,
quisque ut semper leo; Nam porta risus et dui molestie sodales. Sed ullamcorper, orci ut
suscipit cursus, ipsum ipsum scelerisque velit, vel facilisis ligula urna eu felis.
Quisque sed orci eu felis rhoncus fermentum lobortis quis lorem. Nulla hendrerit, ligula
semper sagittis viverra, quam lacus condimentum augue, ut varius augue orci et velit.
Praesent tellus erat, vulputate non congue non, sagittis vel quam. Integer orci metus,
dapibus at suscipit scelerisque, ultrices at neque. Etiam fermentum molestie diam ac
semper. Donec convallis tincidunt augue, vel tincidunt mi aliquam ut. Maecenas rutrum ante
eget mauris lobortis ut consectetur neque congue.

Output:
"augue lectus, mattis nec tempus vel, mattis vel sapien, morbi venenatis faucibus   pulvinar,"
quisque ut semper leo;"
"eget mauris lobortis ut consectetur neque congue."

Try this: 尝试这个:

while($text =~ /(?:(?<!.)|\n)([aeuoi][^;.]*[;.])/img)
{
    print "$1\n";
}

Your regex works, but you never set $text , nor do you print the match. 您的正则表达式有效,但是您从未设置$text ,也没有打印匹配项。

You could do it like this: (ideone demo) 您可以这样做: (ideone演示)

use strict;
use warnings;

my $text = join "", <>;

while($text =~ /^[aeiou][^.;]*[.;]/mig)
{
        print "$&\n";
}

Output: 输出:

augue lectus, mattis nec tempus vel, mattis vel sapien, morbi venenatis faucibus pulvinar,
quisque ut semper leo;
eget mauris lobortis ut consectetur neque congue.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM