Wednesday, June 17, 2009

PERL PROBLEM 7 (HARD)

Make a pattern that matches any line of input that has the same word repeated two or more times in a row. Words in this problem can be considered to be sequences of letters a to z or A to Z, digits, and underscores. Whitespace between words may differ. For example, the classic observation-test string Paris in the the spring should match, since it has a doubled word. Also, I thought that that was the problem should match, even though that may be a correct use of a doubled word. Does your pattern match all three words in I thought that that that was the problem (with extra spaces between only some of the words)? Does it match This is a test? How about This shouldn't match, according to the theory of regular expressions?


(FROM LEARNING PERL)

Thankyou

Teja

2 comments:

  1. /usr/local/bin/perl -w

    my @string=();

    $string[0]="Hello bio world world world.";
    $string[1]="the classic observation-test string Paris in the the spring should match, since it has a doubled word";
    $string[2]="Every gun makes its own tune.";

    foreach my $str ( @string ) {
    if ( $str =~ /\b(\w+)(\s+\1)+\b/ ) {
    print "\n $str \n";
    }
    }

    ReplyDelete