Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

treating * as a normal character in a regex

by chuleto1 (Beadle)
on Jul 29, 2002 at 17:43 UTC ( [id://186030]=perlquestion: print w/replies, xml ) Need Help??

chuleto1 has asked for the wisdom of the Perl Monks concerning the following question:

$line = "THIS TEXT HAS A * IN IT"; $theMatch = "THIS TEXT HAS A * IN IT"; if($theMatch =~ /$line/ ) { s/$line/$tag/g; }
I would like to know how to have match operator "match" the * as a char and not as an regexp operator. Current implementation interprets the * as a regexp operator.

thanks for you wisdom

edited: Mon Jul 29 20:29:42 2002 by jeffa - added code tags

Replies are listed 'Best First'.
Re: regexp =~
by japhy (Canon) on Jul 29, 2002 at 18:05 UTC
    First, you want to use /\Q$line\E/. Second, why are you doing if (/$x/) { s/$x/$y/g } instead of the simpler s/$x/$y/g?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: regexp =~
by redsquirrel (Hermit) on Jul 29, 2002 at 17:50 UTC
    ... if($theMatch =~ /\Q$line\E/ ) ...
    The \Q and \E start and stop metacharacter escaping, respectively.

    --Dave

Re: regexp =~
by thelenm (Vicar) on Jul 29, 2002 at 17:50 UTC
    You should use \Q and \E, which put backslashes before special characters. The regex should look something like this:
    $theMatch =~ /\Q$line\E/
    Also, are you trying to perform a substitution on $theMatch or $line? You're actually performing a substitution on $_ (which is probably not what you want).

    -- Mike

    --
    just,my${.02}

Re: regexp =~
by talexb (Chancellor) on Jul 29, 2002 at 17:49 UTC
    Have you tried escaping the '*' with a '\' in your regexp?

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!"
    --Michael Flanders and Donald Swann

Re: regexp =~
by kvale (Monsignor) on Jul 29, 2002 at 17:50 UTC
    To quote regexp metacharacters, use the  \Q ... \E quoting ops:
    if($theMatch =~ /\Q$line\E/ )
    -Mark
Re: regexp =~
by BrowserUk (Patriarch) on Jul 29, 2002 at 17:49 UTC

    Escape the star eg. "THIS TEXT HAS A \* IN IT";

    Baskslash will escape special chars in a regex! If your regex string is not going to be interpolated twice. Otherwise it's definately best to ignore this monk's advice and skip to the next answer:)

    UpdateGrrr. It worked fine in my test program ..mutter mutter mutter...:(

    C:\test>perl -w "$_ = 'aaaaaaaaaaaaaa*bbbbbbbbbbb'; print 'Found it!' +if (/\*/);" Can't open perl script "$_ = 'aaaaaaaaaaaaaa*bbbbbbbbbbb'; print 'Foun +d it!' if (/\*/ );": Invalid argument C:\test>perl -we "$_ = 'aaaaaaaaaaaaaa*bbbbbbbbbbb'; print 'Found it!' + if (/\*/);" Found it! C:\test>perl -we "$_ = 'aaaaaaaaaaaaaabbbbbbbbbbb'; print 'Found it!' +if (/\*/);" C:\test>

    Shame I felt so silly that I (had to) tested it, that I chose not to post the horribly complicated test prog.

    <cockney>Say la vee </cockney>:)

    And yes, I 'wasted' a vote and ++ Mr. AM.

    That guy amazes me, one time s/he asks the most trivial of questions, other times s/he responds with such knowledgable answers. Sometimes for several sometimes on the same day.

    It's almost like s/he has a split personality??

      Escape the star eg. "THIS TEXT HAS A \* IN IT";

      Note, that will not fix the problem. Try it and see. You need to use single quotes and a backslash, double quotes and two backslashes, or better, \Q and \E as suggested by others.

Re: regexp =~
by Nightblade (Beadle) on Jul 29, 2002 at 17:50 UTC
    please read perlre manpage about regexps.

    * must be escaped using slash \*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186030]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found