Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Easy one for my fellow Monks

by ddrumguy (Acolyte)
on Dec 20, 2001 at 02:19 UTC ( [id://133288]=perlquestion: print w/replies, xml ) Need Help??

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

How can i replace a ( with ,(

I tried this and it causes errors  $input =~ s/(/,(/;

Replies are listed 'Best First'.
Re: Easy one for my fellow Monks
by Chmrr (Vicar) on Dec 20, 2001 at 02:26 UTC

    This is because the open paren has a special meaning in regular expressions -- it tells Perl that you may want to talk about whatever ends up matching between the parens later. Because you want a literal paren, you need to escape it. Try s/\(/,(/;

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Q: Why wouldn't it be s/\(/,\(/; ? Don't you have to escape the second open paren as well?

      If things get any worse, I'll have to ask you to stop helping me.

        Parens are special characters to regular expressions. Only the first part of s/// is a regular expression. The replacement part is a string. Parens aren't special in strings (though $ and @ are, for example, so you'd have to escape those if you wanted a literal $ or @).

                - tye (but my friends call me "Tye")
Re: Easy one for my fellow Monks
by Anonymous Monk on Dec 20, 2001 at 04:27 UTC
    You must escape parens and these \, |, [, {, ^, $, *, +, ?, . $input =~ s/\(/,\)/; remember to use a /g at the end to match multiple times on one line.
      If you are doing several metachars you can also use \Q to quote your metachars.

      $var =~ s#\Q(#,(#;
      or
      $var =~ s#\Q([]\E#/meta/;


      /Q will quote all the metachars it sees in a regex until \E.
Re: Easy one for my fellow Monks
by ddrumguy (Acolyte) on Dec 20, 2001 at 20:31 UTC
    THANK YOU ALL ! works like a charm - you guys were very helpful. Someday I will be better at this - (only been doin Perl for a few weeks now). thanks again bob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found