dreamy has asked for the wisdom of the Perl Monks concerning the following question:

i'm looking for a smart
solution for following problem:
my $a = qq(foo bar foo bar foo bar foo bar foo bar); $a =~ s/HELP HERE{4}.+/$1.../g; # <<< this is the problem print $b;
it should return:

foo bar foo bar...

can this be done with one nice regex ? :-)

Replies are listed 'Best First'.
Re: get first e.g. 5 words and replace the remaining string with ... with one regex
by LAI (Hermit) on Jan 28, 2003 at 14:26 UTC

    You probably want to do something like:

    my $a = qq(foo bar foo bar foo bar foo bar foo bar); $a =~ s/((\w+\s+){4}).+/$1.../g; print $a; __END__ output: foo bar foo bar ...

    This way, any words past the fourth are replaced by "...", and if there are fewer than four words no replacement happens.


    LAI
    :eof
Re: get first e.g. 5 words and replace the remaining string with ... with one regex
by bart (Canon) on Jan 28, 2003 at 14:46 UTC
    Does it have to be a regex? You can use split, if you don't mind the whitespace being mangled up. I guess you don't really care.
    my @words = split / /, $string, 6; $words[-1] = '...' if @words==6; $string = join ' ', @words;
    Hmm... that does add a space before the final ellipsis. A beauty spot, or a fatal flaw?
Re: get first e.g. 5 words and replace the remaining string with ... with one regex
by diotalevi (Canon) on Jan 28, 2003 at 14:36 UTC

    That's kind of an odd requirement. Is this homework? It makes more sense to just extract the first five words and then append whatever you want. The way you posed it awkward and should be reframed.

    Hmmm... on further thought the expanded code with the conditional really just looks like a plain replacement. I've added a second code block where I turn this into a substitution.

    The original code. This will clobber $text if there are less than five words.

    # Capture the first five words including whitespace. ($text) = $text =~ / (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/x; # Shrink any internal whitespace $text =~ s/\s+/ /g;

    The amended code to compensate for the previous block's clobbering action

    # Capture the first five words including whitespace. if ( $text =~ / (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/x ) { # The regex extracted the first five words $text = "$1 ..."; # Shrink any internal whitespace $text =~ s/\s+/ /g; }

    That previous block really looked like a substition. Here it is, reframed again

    if ( $text =~ s/ (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/$1 .../x ) { # The match succeeded, shrink the white space $text =~ s/\s+/ /g; }

    Updated Altered $text .= "whatever" to $text .= " ...". Also added caveats on word count


    Seeking Green geeks in Minnesota

Re: get first e.g. 5 words and replace the remaining string with ... with one regex
by robartes (Priest) on Jan 28, 2003 at 14:29 UTC
    How's this:
    #!/usr/local/bin/perl -w use strict; my $text="Twenty centuries of stony sleep were vexed to nightmare"; my $numwords=5; my $replacement="lead to a crick in the back."; print $text; $text=~ s/^((\w+\s){$numwords}).*$/$1.$replacement/e; print $text; __END__ Twenty centuries of stony sleep were vexed to nightmare Twenty centuries of stony sleep lead to a crick in the back.
    perlre is your friend.

    CU
    Robartes-