Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to ignore special characters in a variable ?

by Anonymous Monk
on Feb 15, 2011 at 18:17 UTC ( #888341=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am searching presence of $string in $line

my $line = "CCCC--CCCC--CCC(--CC(O"; my $string = "CCC("; if ($line =~ m/$string/) {print "match";}
This code is part of a larger script, and, at times, I do not even know what special character is present in $string. Is there a way to ignore special characters in a variable? Some thing like "/g" global option or "/i" ignore case option :-)

Thanks in Advance

Comment on How to ignore special characters in a variable ?
Download Code
Re: How to ignore special characters in a variable ?
by Fletch (Chancellor) on Feb 15, 2011 at 18:23 UTC

    Put your hands over your ears, stomp your feet, and say loudly "LA LA LA I'M IGNORING YOU SPECIAL CHARACTERS!"

    Or check out quotemeta and/or \Q\E. Whichever.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thank you very much
Re: How to ignore special characters in a variable ?
by toolic (Chancellor) on Feb 15, 2011 at 18:23 UTC
    quotemeta
    use warnings; use strict; my $line = "CCCC--CCCC--CCC(--CC(O"; my $string = quotemeta "CCC("; if ($line =~ m/$string/) {print "match";}
      Thanks. It works !!
Re: How to ignore special characters in a variable ?
by wind (Priest) on Feb 15, 2011 at 18:51 UTC
    quotemeta or \Q...\E
    my $line = "CCCC--CCCC--CCC(--CC(O"; my $string = "CCC("; if ($line =~ m/\Q$string\E/) {print "match";}

      /Q.../E turns out to be better option than quotemeta. Just in case a beginner happens to read this node - remember - quotemeta modifies your string by adding "\" before special characters. If your string is a variable (like $string in above example), it becomes unusable.

      Having fun with perl... :-)
        /Q /E isn't real syntax, but \Q \E is :)

        \Q is quotemeta, so I don't see how it could be better.

        >perl -MO=Concise,-exec -e"qq{\Q$x}" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <1> quotemeta[t2] sK/1 5 <@> stringify[t3] vK/1 6 <@> leave[1 ref] vKP/REFC -e syntax OK >perl -MO=Concise,-exec -e"quotemeta($x)" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <1> quotemeta[t2] vK/1 5 <@> leave[1 ref] vKP/REFC -e syntax OK

        If you're just talking about how it looks in the code, ok.

Re: How to ignore special characters in a variable ?
by TomDLux (Vicar) on Feb 16, 2011 at 16:31 UTC

    You want to know whether $string appears in $line, and possibly, where within the line it appears. This exactly matches the specification of the index() builtin routine. Try typing perldoc -f index into an xterm window, or check out CPAN's search facility.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Thanks.

      btw I loved the quote "Entia non sunt multiplicanda praeter necessitatem".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://888341]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-10-21 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls