String Substitution question

L

LtCommander

This could be a stupid question but I am really stuck!

I am doing
$content=~ s/$m//sig;

The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:

$m =~ s/\(/\\(/sig;
$m =~ s/\)/\\)/sig;
$m =~ s/\[/\\[/sig;
$m =~ s/\]/\\]/sig;
$m =~ s/\?/\\?/sig;
$m =~ s/\*/\\*/sig;
$m =~ s/\./\\./sig;
$m =~ s/\$/\\\$/sig;
$m =~ s/\+/\\+/sig;
$m =~ s/\_/\\_/sig;
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;

Is there a better way of doing this?

Thanks a lot for any help.

VInce
 
M

Mirco Wahab

LtCommander said:
I am doing
$content=~ s/$m//sig;

The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:

$m =~ s/\(/\\(/sig;
...
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;

try:

my @stuff = qw'( ) [ ] ? * . $ + _ -';
my $m = join '|', map "\Q$_\E", @stuff;

$content =~ s/$m//g;


maybe it does what you want ...

Regards

M.
 
M

Mirco Wahab

Mirco said:
LtCommander said:
I am doing
$content=~ s/$m//sig;

The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:

$m =~ s/\(/\\(/sig;
...
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;

try:

my @stuff = qw'( ) [ ] ? * . $ + _ -';
my $m = join '|', map "\Q$_\E", @stuff;

$content =~ s/$m//g;

the above looks somehow inefficient after a moment - ,
so better try (Perl's '|' alternation is slow)

my @stuff = qw'( ) [ ] ? * . $ + _ -';
my $m = '[' . (join '', map quotemeta, @stuff) . ']';

$content =~ s/$m//g;


Regards

M.
 
P

Paul Lalli

This could be a stupid question but I am really stuck!

I am doing
$content=~ s/$m//sig;

The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:

$m =~ s/\(/\\(/sig;
$m =~ s/\)/\\)/sig;
$m =~ s/\[/\\[/sig;
$m =~ s/\]/\\]/sig;
$m =~ s/\?/\\?/sig;
$m =~ s/\*/\\*/sig;
$m =~ s/\./\\./sig;
$m =~ s/\$/\\\$/sig;
$m =~ s/\+/\\+/sig;
$m =~ s/\_/\\_/sig;
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;

Is there a better way of doing this?

Dear god yes.

Just do:
$content =~ s/\Q$m\E//sig;

Look up what that does in:
perldoc -f quotemeta
perldoc perlre

Paul Lalli
 
T

Tad McClellan

Lawrence Statton XE2/N1GAK said:
LtCommander said:
$m =~ s/\(/\\(/sig;
$m =~ s/\)/\\)/sig;
$m =~ s/\[/\\[/sig;
$m =~ s/\]/\\]/sig;
$m =~ s/\?/\\?/sig;
$m =~ s/\*/\\*/sig;
$m =~ s/\./\\./sig;
$m =~ s/\$/\\\$/sig;
$m =~ s/\+/\\+/sig;
$m =~ s/\_/\\_/sig;
$m =~ s/\-/\\-/sig;

OH! I see it, it's a pony!


Bzzzt! It's a camel.

With two humps.
 
G

Guest

This could be a stupid question but I am really stuck!

I am doing
$content=~ s/$m//sig;

The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:

$m =~ s/\(/\\(/sig;
$m =~ s/\)/\\)/sig;
$m =~ s/\[/\\[/sig;
$m =~ s/\]/\\]/sig;
$m =~ s/\?/\\?/sig;
$m =~ s/\*/\\*/sig;
$m =~ s/\./\\./sig;
$m =~ s/\$/\\\$/sig;
$m =~ s/\+/\\+/sig;
$m =~ s/\_/\\_/sig;
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;

Is there a better way of doing this?

Thanks a lot for any help.

VInce

Yes:

$content =~ s/\Q$m\E//ig;

Read the docs:
perldoc perlre
perldoc -f quotemeta
 
D

Dr.Ruud

Mirco Wahab schreef:
my @stuff = qw'( ) [ ] ? * . $ + _ -';
my $m = '[' . (join '', map quotemeta, @stuff) . ']';

Alternatives:

my $m = q:[: . quotemeta( q:()[]?*.$+_-: ) . q:]: ;


$ perl -wle'
my $m = q:][()?*.$+_-: ;
print qr/[$m]/
'
(?-xism:[][()?*.$+_-])
 
M

Mirco Wahab

Dr.Ruud said:
Alternatives:
my $m = q:[: . quotemeta( q:()[]?*.$+_-: ) . q:]: ;

Of course, dropping the @stuff makes things smaller ;-)
$ perl -wle'
my $m = q:][()?*.$+_-: ;
print qr/[$m]/
'
(?-xism:[][()?*.$+_-])

OK, but wouldn't this throw an 'unmatched ] bracket'
or something when used on strings with '] ['

No, it doesn't. I did *not* expect this would work:

my $content = '
Hello ( zszs ) hs $ jjs + owow - kshs ? hhshs [ tetet [] hshs!
';

my $m = '][()?*.$+_-';
$content =~ s/[$m]//g;
print $content;


.... and still roll my eyes!

Thanks for clearing this up,

M.
 
L

LtCommander

Thank you all for replying. This seems very basic yet I guess you miss
some things when you are self-taught.
Also, thanks for the pony + camel humor!!

Vince

This could be a stupid question but I am really stuck!
I am doing
$content=~ s/$m//sig;
The problem is that $m may have all sorts of escape sequence
characters, brackets and so on. So, sometimes, $content doesn't have
$match removed. So, I do this:
$m =~ s/\(/\\(/sig;
$m =~ s/\)/\\)/sig;
$m =~ s/\[/\\[/sig;
$m =~ s/\]/\\]/sig;
$m =~ s/\?/\\?/sig;
$m =~ s/\*/\\*/sig;
$m =~ s/\./\\./sig;
$m =~ s/\$/\\\$/sig;
$m =~ s/\+/\\+/sig;
$m =~ s/\_/\\_/sig;
$m =~ s/\-/\\-/sig;
and then
$content=~ s/$m//sig;
Is there a better way of doing this?
Thanks a lot for any help.
VInceYes:

$content =~ s/\Q$m\E//ig;

Read the docs:
perldoc perlre
perldoc -f quotemeta
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,483
Members
48,146
Latest member
Dino69J579

Latest Threads

Top