in reply to Regular Expression, substitution

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?

Replies are listed 'Best First'.
Re^2: Regular Expression, substitution
by GrandFather (Saint) on Apr 06, 2016 at 03:57 UTC

    \b matches a word boundary. It's a zero width anchor. It works like qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/ which is likely to blow your mind unless you are comfortable with look around matches.

    Premature optimization is the root of all job security

      Actually, it's trickier than that.  qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/ requires a character before and after the assertion, whereas  \b can match at the start and end of a string. A kind of double-negative is needed in an equivalent look-around:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xx-xx'; ;; my $lbw = qr/(?<=\w)(?=\W)|(?<=\W)(?=\w)/; printf qq{$-[0] } while $s =~ m{ $lbw }xmsg; print qq{\n}; ;; my $wb = qr{ (?<!\w)(?!\W) | (?<!\W)(?!\w) }xms; printf qq{$-[0] } while $s =~ m{ $wb }xmsg; print qq{\n}; ;; printf qq{$-[0] } while $s =~ m{ \b }xmsg; " 2 3 0 2 3 5 0 2 3 5

      Update: Now,  \B is another story...


      Give a man a fish:  <%-{-{-{-<

      Thanks! I was just reading up on it so I could answer my own question.

      I can't imagine how I've gone decades not having bumped into it.