Created
March 25, 2015 16:56
-
-
Save gknoy/94c382299c4543f2e863 to your computer and use it in GitHub Desktop.
spaces-to-tabs: Simple regex sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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