Better Regrex
------=_NextPart_000_003C_01CBE931.01C443A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
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 =3D "Why are we here? To bless, inspire and uplift one =
another. #TRB #inspiration #loa";
if($message =3D~ /\#(.*)/i) { $marker =3D $1; }
$message =3D~ s!$mark!!gi;
$message =3D~ 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.
Thx's
Mike(mickalo)Blezien
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- =3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D-=3D-=3D-=3D
Thunder Rain Internet Publishing
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D -=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-
------=_NextPart_000_003C_01CBE931.01C443A0--
Re: Better Regrex
--002354530970218aed049f2686dc
Content-Type: text/plain; charset=ISO-8859-1
Regex, or maybe regexp, but not regrex.. That doesn't make any sense : p
I have no idea what you mean with "section in red." However, there's a few
things wrong with your code:
What is $mark? Don't you mean $marker? As it stands, your snipped would die
under strict, and in non-strict $mark would be undefined, which would be
interpreted as an empty string; You'd be replacing all empty strings with
empty strings, effectively achieving nothing at the cost of doing a lot.
Also, that if is pretty worthless if all you using it for is to assign $1
you might as well write my ($marker) = message =~ /regex here/;
But assuming you mean "delete all words that start with a #," then yeah,
there's a much simpler way:
> s/#\w*//g;
Which uses the /g modifier, explained in perlretut[0] and perlop[1].
Meanwhile, if you mean "if a word start with #, delete it from everywhere in
the script", you could do something like
> s/$+{word}//g while s/ \# (?<word> \w+ ) //x;
>
This uses named captures, explained in perlretut and perlre[2]. You could do
without them though.
Brian.
[0] http://perldoc.perl.org/perlretut.html#Using-regular-express ions-in-Perl
[1] http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operat ors
[2] http://perldoc.perl.org/perlre.html
On Wed, Mar 23, 2011 at 10:05 AM, Mike Blezien <mickalo [at] 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.
>
> Thx's
> Mike(mickalo)Blezien
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Thunder Rain Internet Publishing
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
--002354530970218aed049f2686dc--
Re: Better Regrex
On Wed, Mar 23, 2011 at 09:05, Mike Blezien <mickalo [at] frontiernet.net> wrote=
:
> Hello,
>
> I'm working on a simple regrex issue which I got to work but I think ther=
e's a better way to do this. This is what I have right now. I need to simpl=
y remove the string section in red.
>
> my($marker);
> my $message =3D "Why are we here? =C2=A0To bless, inspire and uplift one =
another. #TRB #inspiration #loa";
> =C2=A0 if($message =3D~ /\#(.*)/i) { $marker =3D $1; }
> =C2=A0 $message =3D~ s!$mark!!gi;
> =C2=A0 $message =3D~ s!\#!!gi;
>
> #Resulting String wanted:
> =C2=A0Why are we here? =C2=A0To 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 sugges=
ted would be appreciated.
snip
Removal can be done with one substitution:
$message =3D~ 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 =3D "Why are we here? To bless, inspire and uplift one
another. #TRB #inspiration #loa";
my [at] markers =3D $message =3D~ /#(\S+)/g;
$message =3D~ s/\s*#\S+\s*//g;
print "[$message]\nmarkers: ", join(", ", [at] markers), "\n";
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Better Regrex
----- Original Message -----
From: "Chas. Owens" <chas.owens [at] gmail.com>
To: "Mike Blezien" <mickalo [at] frontiernet.net>
Cc: "Perl List" <beginners [at] perl.org>
Sent: Wednesday, March 23, 2011 8:42 AM
Subject: Re: Better Regrex
On Wed, Mar 23, 2011 at 09:05, Mike Blezien <mickalo [at] 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 [at] markers = $message =~ /#(\S+)/g;
$message =~ s/\s*#\S+\s*//g;
print "[$message]\nmarkers: ", join(", ", [at] markers), "\n";
=========================================
Thx's this work very well, allot cleaner.
Mike
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/