[TxMt] regex question

Allan Odgaard throw-away-1 at macromates.com
Wed Feb 22 09:35:50 UTC 2006

On 22/2/2006, at 9:21, Charilaos Skiadas wrote:

>> But when I run the command none of the tabs or newlines I've  
>> specified in the replace section of the command are applied to the  
>> text in question [...]
> [...]
> 1) It searches each line separately, in which case it won't match  
> the multiline search you are performing

That’s exactly it! :)

The -p switch puts a loop around the perl code, which execute that  
code for each line in the input (stdin).

So: perl -pe 'foo' translates to:

    for each line in the input
       print result of running “foo” on line

Loops and such are generally easier in Ruby, so instead try:

ruby -e '
	print STDIN.read.gsub(

So what’s s/«regexp»/«replacement»/ in Perl is sub(/«pattern»/,  
«replacement») in Ruby. And instead of adding ‘g’ as option to make  
it “global”, we use gsub.

In addition, in Ruby we have to use \1 instead of $1 etc.

In Perl the s/«regexp»/«replacement»/ runs on the $_ variable (which  
is the current line, when used with the -p switch). Ruby has a  
similar feature (both -p and $_ as “accumulator” reguster), but in  
the above we explicitly call gsub on all we read from STDIN, instead  
of doing it line-by-line.

More information about the textmate mailing list