如何通过模式匹配提取单词?

时间:2013-08-18 14:43:14

标签: perl

#!/usr/bin/perl

use strict;
use warnings;
my $string = "praveen is a good boy";
my @try = split(/([a,e,i,o,u]).*\1/,$string);
print "@try\n";

我正在尝试打印给定字符串中包含2个相邻元音的所有单词 o / p:必须是“praveen”和“good”。

我尝试使用否定exp [^]来分割,只给出2个相邻的元音。

3 个答案:

答案 0 :(得分:10)

Perl函数split不适合查找匹配列表。相反,我建议使用正则表达式修饰符g。要处理所有匹配项,您可以循环使用例如while,或者您可以一次性分配匹配列表。

以下示例应匹配字符串中包含两个相邻元音的所有单词:

my $string = "praveen is a good boy"; 
while ( $string =~ /(\w*[aeiou]{2}\w*)/g ) { 
  print "$1\n" 
}

输出:

praveen
good

你也可以这样做:

my @matches = ( $string =~ /\w*[aeiou]{2}\w*/g );

并处理结果,类似于您在OP中处理@try的方式。

答案 1 :(得分:6)

你可以做点像......

#!/usr/bin/perl

use strict;
use warnings;

my $str 
   = "praveen is a good boy\n"
   . "aaron is a good boy\n"
   . "praveen and aaron are good, hoot, ho"
   ;

while ($str =~ /(\w*([aeiou])\2(?:\w*))/g) {
       print $1, "\n";
}

正则表达式:

(               group and capture to \1:
 \w*            word characters (a-z, A-Z, 0-9, _) (0 or more times)
   (            group and capture to \2:
    [aeiou]     any character of: 'a', 'e', 'i', 'o', 'u'
   )            end of \2
   \2           what was matched by capture \2
    (?:         group, but do not capture:
      \w*       word characters (a-z, A-Z, 0-9, _) (0 or more times)
    )           end of grouping
)               end of \1

这与执行/(\w*([aeiou])[aeiou]+(?:\w*))/

基本相同

输出:

praveen
good
aaron
good
praveen
aaron
good
hoot

答案 2 :(得分:3)

#!/usr/bin/perl

use strict;
use warnings;
my $string = "praveen is a good boy";
my @try = split(/\s/,$string);
for(@try) {
#    if(/[a,e,i,o,u]{2}/) {
    if(/[aeiou]{2}/) { # edited after Birei's comment
        print "$_\n";
    };
};

“分裂”的第一个参数是分隔符。拆分(-8