Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

treating * as a normal character in a regex

by chuleto1 (Beadle)
on Jul 29, 2002 at 17:43 UTC ( #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.


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


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/ )
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?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2023-03-23 04:27 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.