Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

end of line anchor in regex

by redss (Monk)
on Dec 09, 2007 at 15:59 UTC ( [id://655965]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

I want to filter out words at the end of a line but it's not working. Why doesn't the below code filter out the word hello at the end of the line?

$text=<<EOT; hello world EOT $text =~ s/hello$//g; print $text;

Replies are listed 'Best First'.
Re: end of line anchor in regex
by FunkyMonk (Chancellor) on Dec 09, 2007 at 16:20 UTC
    You can use the /m modifier to make $ match newlines: $text =~ s/hello$//gm;

    See perlre for more information

Re: end of line anchor in regex
by jrsimmon (Hermit) on Dec 09, 2007 at 16:12 UTC
    There's nothing wrong with your substitution. You're string doesn't end with the word "hello", so it doesn't match and no substitution is made.
Re: end of line anchor in regex
by dwm042 (Priest) on Dec 09, 2007 at 16:50 UTC
    As others have already pointed out, the variable $text doesn't end in 'hello', so the substitution never occurs.

    If you reorganized the program so that the data were read in from a file, or from a __DATA___ statement following the program, it works just fine, as so:

    #!/usr/bin/perl use warnings; use strict; while (my $text = <DATA>) { $text =~ s/hello$//g; print $text; } __DATA__ hello world
    The output is:

    ~/perl/perlmonks$ ./nohello.pl world
Re: end of line anchor in regex
by ikegami (Patriarch) on Dec 09, 2007 at 18:52 UTC

    By default (no "m" option),
    $ matches at a newline at the end of the string.
    $ matches at the end of the string.

    With the "m" option (s///m),
    $ matches at the end of a line (at a newline).
    $ matches at the end of the string.

Re: end of line anchor in regex
by sh1tn (Priest) on Dec 09, 2007 at 17:39 UTC
      less efficient, less maintainable, with a warning, and with a pitfall: what if a word other than the word to be deleted is composed entirely of letters in the targeted word. e.g.:

      perl -wMstrict -e "my $text = qq(start\nrats\n); $text = join($1, grep(/[^start]/, split(/(\n)/, $text))); print qq(o/p: \n); print qq({$text})" Use of uninitialized value in join or string at -e line 1. o/p: { }

      wouldn't it at least be better to say  grep($_ ne 'start', ... ) (although it still leaves the "Use of uninitialized value ..." problem, which i don't quite understand)?

        ...although it still leaves the "Use of uninitialized value ..." problem, which i don't quite understand.
        my confusion stemmed from my bemusement with an imagined connection between the (\n) expression in the split regex and the $1 capture variable in the join and the idea that $1 must contain a newline at that point.

        then it occurred to me that the intervening grep regex would undefine all the capture variables and, since it had no capturing parentheses of its own, leave them that way.

        but it goes beyond that.   apparently, "capturing" parentheses in a split regex don't actually capture (at least not to the capture variables), so $1 was never defined at any point.

        perl -wMstrict -e "my @ra = split /(c)/, 'abcde'; print '$1 ', defined $1 ? 'defined' : 'undefined', qq( @ra)" $1 undefined ab c de

      Captures in the split regex does have an effect, but setting $1 shouldn't be relied on. The effect of captures in the split regex is to return the captured data.

      split(/\n/, "hello\nworld") -> ("hello", "world") split(/(\n)/, "hello\nworld") -> ("hello", "\n", "world")

      I'd recommend

      $text = join '', grep $_ ne "$word\n", split /(?<=\n)/, $text;

      if you wanted to use split.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://655965]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found