DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Escape A Character With Backslashes

09.16.2006
| 6091 views |
  • submit to reddit
        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;