Skip to content

Instantly share code, notes, and snippets.

@gknoy
Created March 25, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gknoy/94c382299c4543f2e863 to your computer and use it in GitHub Desktop.
Save gknoy/94c382299c4543f2e863 to your computer and use it in GitHub Desktop.
spaces-to-tabs: Simple regex sample
#! /usr/bin/perl
#
# spaces-to-tabs-filter
#
# A simple regex example
# perl -e 'while (<>) { $line = $_; $line =~ s/ /\t/g; print $line; }' < foo_spaces.py > foo_tabs.py
#
while (<>) {
$line = $_;
# - Replace the string " " (four spaces) with a tab character
# - 'g' at the end means "Do this globally" -- ie, replace all occurrences.
$line =~ s/ /\t/g;
# The / character is the traditional regex delimiter in Perl.
# Perl lets you use any character (?) for this,
# which can be convenient to avoid escaping things.
# These are equivalent:
# Replace \ with / characters:
# $line =~ s/\//\\/g; # here we escape both / and \ characters
# $line =~ s!\\!/!g; # ... though we only have to escape \ chars.
print $line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment