Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to write regex for special characters without using back slashes??

by sumathigokul (Acolyte)
on May 22, 2015 at 05:39 UTC ( [id://1127430]=perlquestion: print w/replies, xml ) Need Help??

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

Hi perl monks...

Please suggest me how to search and substitute for word with special characters without using back slashes..

my @nets = "in1_[0], in1_[1]";

since variable nets contains auto generated words, sometimes it may contain simple words without any special variables as follows

my @nets = "a_c, b_c, N_05";

lets say my file has in1_[0] word at many lines of codes and i wrote a perl script to search for in1_[0] i.e. $nets[0] word and replace it with in1_1 word i.e. $nets1...

while (<DATA>){ print "substitution is done" if (s/$nets[0]/$nets[1]/); }

But, while executing the script i understood that special characters [] are present in variable nets, hence this substitution regex statement is not working... i read that i have to use back slashes for those characters... since nets file will be auto generated and sometimes only it may contain array values, how can i generalize this regex script accordingly...

-DATA- in1_c[0] in1_c[1] in1_c[0] in1_c[1] a_c in1_c[0] b_c in1_c[1] in1_c[0] in1_c[1]

Replies are listed 'Best First'.
Re: How to write regex for special characters without using back slashes??
by 2teez (Vicar) on May 22, 2015 at 06:14 UTC

    could quotemeta and or \Q ... \E be what you are looking for?

    I have not tested this on the OP given data.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: How to write regex for special characters without using back slashes??
by Corion (Patriarch) on May 22, 2015 at 06:23 UTC

    See perlre, especially \Q and \E.

      Hi all... I could achieve using \Q..\E...Thank you for sharing your knowledge...

Re: How to write regex for special characters without using back slashes??
by soonix (Canon) on May 22, 2015 at 09:13 UTC

    I don't quite understand your requirements. Possibly this is because English is not my native language, but I fear it is unclear even to the native speakers.
    My impression is, that you haven't yet thought it completely through yourself.

    I recommend the teddy bear approach, that is, try to explain to a random person, who should   not   be an expert, what it is you want to do and how you think it should be done.

    Update 2015-07-02: I just learnt that the "official" name of this technique is rubber duck debugging.
Re: How to write regex for special characters without using back slashes??
by afoken (Chancellor) on May 22, 2015 at 07:25 UTC

    Still abusing perlmonks as a code writing service? Please don't create new discussion threads for your problem, continue in the old ones and READ THE FINE ANSWERS!

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-05-30 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found