Using your first example:

#!/usr/bin/perl use strict; use warnings; $currentSentence =~ s/[\ba\b|\ban\b|\bthe\b]//g; exit;

I get the following:

P:\>rmv1.pl Global symbol "$currentSentence" requires explicit package name at P:\ +rmv1.pl line 4. Execution of P:\rmv1.pl aborted due to compilation errors.

Then I fixed the error on line 4:

#!/usr/bin/perl use strict; use warnings; my $currentSentence =~ s/[\ba\b|\ban\b|\bthe\b]//g; exit;

Which yields the following:

P:\>rmv2.pl Use of uninitialized value $currentSentence in substitution (s///) at +P:\rmv2.pl line 4.

So, to fix that, I added a value based on your loose description:

#!/usr/bin/perl use strict; use warnings; my $currentSentence = "The big dog rolled in an open field filled with + a type of grass."; $currentSentence =~ s/[\ba\b|\ban\b|\bthe\b]//g; exit;

I get the following:

P:\>rmv3.pl P:\>

Now morbidly curioius, I added a line to display the result:

#!/usr/bin/perl use strict; use warnings; my $currentSentence = "The big dog rolled in an open field filled with + a type of grass."; $currentSentence =~ s/[\ba\b|\ban\b|\bthe\b]//g; print "[$currentSentence]\n"; exit;

I get the following:

P:\>rmv4.pl [T big dog rolld i op fild filld wi yp of grss.]

I'm now going to ask the question:

What's with all the \baction in your regular expression?


In reply to Re: Regular Expression, substitution by marinersk
in thread Regular Expression, substitution by lobs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.