On Wed, Mar 23, 2011 at 09:05, Mike Blezien <mick...@frontiernet.net> wrote:
> Hello,
>
> I'm working on a simple regrex issue which I got to work but I think there's 
> a better way to do this. This is what I have right now. I need to simply 
> remove the string section in red.
>
> my($marker);
> my $message = "Why are we here?  To bless, inspire and uplift one another. 
> #TRB #inspiration #loa";
>   if($message =~ /\#(.*)/i) { $marker = $1; }
>   $message =~ s!$mark!!gi;
>   $message =~ s!\#!!gi;
>
> #Resulting String wanted:
>  Why are we here?  To bless, inspire and uplift one another.
>
> The method I'm using above works and we get the results wanted but I was 
> looking at it again and I think there's a better way to do this. Any 
> suggested would be appreciated.
snip

Removal can be done with one substitution:

$message =~ s/\s*#\S+\s*//g;

That will remove any whitespace characters followed by a # followed by
one or more non-whitespace characters and the whitespace that follows
it.

If you want to save the markers, you will need to match first, then remove them:

#!/usr/bin/perl

use strict;
use warnings;

my $message = "Why are we here?  To bless, inspire and uplift one
another. #TRB #inspiration #loa";

my @markers = $message =~ /#(\S+)/g;
$message =~ s/\s*#\S+\s*//g;

print "[$message]\nmarkers: ", join(", ", @markers), "\n";





-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to