Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Escape a character with backslashes (See related posts)

Ruby:
s = "12345"
puts s.gsub(/(2|4)/, '\\\\\1')


PHP:
$s="12345";
print preg_replace("/(2|4)/", '\\\\\1', $s);


Perl:
$s="123456";
$s =~ s/(2|4)/\\\1x/g;
print $s;


You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts