正则表达式:找到第n场比赛

时间:2010-08-26 04:28:22

标签: regex perl

我正在尝试编写一个匹配第n个匹配的正则表达式。因为我是regex的新手,请帮助我 解决这个问题。

输入:

DECLARE numerator NUMBER; BEGIN SELECT x, y INTO numerator, denominator FROM result_table, secondTable WHERE sample_id = 8; delete from xyz where id=17;

首先匹配WHERE或匹配第二个WHERE。

另外请向我建议一些良好的链接:

  1. 贪婪和非贪婪的方法。
  2. 嵌套正则表达式。
  3. 提前致谢。

2 个答案:

答案 0 :(得分:4)

您没有指定语言。我假设你正在使用某种形式的动态脚本语言(Perl,C#,Ruby,Python等),其中正则表达式的概念非常相似。我将向您展示如何在Perl中执行此操作,并且您使用的任何语言都应该类似。

使用Perl,您可以对正则表达式模式进行全局匹配,并返回要操作的列表。使用此列表,您可以通过在返回的列表中使用数组偏移来解决第n个匹配。

考虑:

#!/usr/bin/perl

use strict; use warnings;

# use a block of text; in this case "Grimms' Tales from Gutenburg
GetGrimmFairyTail() unless -e '2591.txt';

my (@m1,@m2);
my $i=1;
open (my $grimms,'<','2591.txt') or die $!;
undef $/; #'slurp' mode
my $tail=<$grimms>;

# How many 'cats' in Grimms' Tails?
my $regex=qr/(^.*?\bcat\b.*$)/mi;

@m1=($tail=~(/$regex/g)); 
print   "\n",
        scalar @m1, " global matches\n";

print   "first match is:\n$m1[0]\n",
        "second is:\n$m1[1]\n",
        "last is:\n$m1[-1]\n", 
        "second to last is:\n$m1[-2]\n\n\n";

#How many dogs?
$regex=qr/(^.*?\bdog\b.*$)/mi;      
foreach my $line (split (/\n/,$tail))  {
     while ($line=~/$regex/g) {
        push @m2, [ "Line $i", $1 ];
     }   
     $i++;
 }      

print scalar @m2, " foreach loop matches\n";        
print   "first match with line number:\n$m2[0][0]: $m2[0][1]\n",
        "second is:\n$m2[1][0]: $m2[1][1]\n",
        "last is:\n$m2[-1][0]: $m2[-1][1]\n", 
        "second to last is:\n$m2[-2][0]: $m2[-2][1]\n\n";       

print "The end!\n\n";

sub GetGrimmFairyTail {
    use Net::ftp;
    use File::Spec;
    print   "\n",
            "Getting \"Grimms' Fairy Tales\" from ", 
            "University of North Carolina, Chapel Hill....\n\n";
    my $host='ibiblio.org';
    my $lf;
    my $ftp=Net::FTP->new($host,DEBUG=>0) 
        or die "Cannot connect to $host: $@";
    $ftp->login("anonymous",'-anonymous@')
          or die "Cannot login ", $ftp->message;  
    my $fn=$lf=$ftp->get("/pub/docs/books/gutenberg/2/5/9/2591/2591.txt")
          or die "get failed ", $ftp->message;    
    my $size= -s $fn;
    my $abspath=File::Spec->rel2abs($fn);
    open (my $fh, '<', $fn) or die "can't open downloaded file: $!";
    my $line_count=0;
    my $rs=$/;
    $line_count++ while <$fh>;
    $/=$rs;
    print   "Success!\n",
            "$size bytes containing $line_count lines downloaded into file:\n",
            "$abspath\n\n" if -e $abspath;
    $ftp->quit;
}

回报是:

87 global matches
first match is:
     CAT AND MOUSE IN PARTNERSHIP
second is:
     THE FOX AND THE CAT
last is:
king got to the bottom, he ordered Cat-skin to be called once more, and
second to last is:
Cat-skin,' said the cook; 'for you always put something into your soup,


41 foreach loop matches
first match with line number:
Line 57:      THE DOG AND THE SPARROW
second is:
Line 665: After he had travelled a little way, he spied a dog lying by the
last is:
Line 9078: crevice of the tree, and the little fellow was jumping about like a dog
second to last is:
Line 8561: pursue it; but hardly had the dog run two steps when it stood before a

The end!

答案 1 :(得分:1)

关于你的第二个问题,即正则表达式的良好链接: -

http://www.regular-expressions.info/

请参阅:Learning Regular Expressions

你会得到关于书籍,链接,正则表达等工具的好建议。