Ajey Kulkarni wrote: > > happy new year to all,.. > i've a qn.,:) > i want to 'insert' an escape character for every '[' & ']' > i will find in a variable.. > Suppose $temp has a word which might contain [ and/or ]. > Eg: if $temp has hello] > the modified temp should have hello\] > > if $value has [hello] i want the result to be \[hello\]. > > Is there a quick one line regex to do this? > i'm able to match the presence of [ & ] > if( (/\[/)|(/\]/) ){ > my $value = $_; > $value =~ s/\[/\\\[/; > $value =~ s/\]/\\\]/; > print $value; > > } > Kinda doing the stuff,but i just checkign out a 1 liner reg-ex.
Won't a two-liner do? You've already written it if so, except that square brackets in the replacement string don't need escaping; a one-liner is possible but much less efficient. Also you don't need the 'if'. Does the code below help? I've also changed the s/// delimiters to brackets to avoid the mess that the slashes and backslashes make. my $value = '[hello]'; for ($value) { s(\[)(\\[)g; s(\])(\\])g; } print $value, "\n"; **OUTPUT \[hello\] Alternatively, if it's OK to escape all non-alphanumeric characters then 'quotemeta' is what you want. my $value = '[hello]'; $value = quotemeta $value; print $value, "\n"; **OUTPUT \[hello\] Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>