Here it is (in Perl):

<code>

use strict;
use warnings;

open FILE, '/path/to/file/'
 or die 'Couldn't open file: $1';

open OUTPUT '>/path/to/output_file/';

while (<FILE>) {
my $line = $_;
# my $line = 'This isA lineWithSome words both in camelCaseAnd not.';

$line =~ s/(\S+?)([A-Z])(\S+?|\s+?)/$1_$2$3/g;

print OUTPUT "$line\n";
}
</code>

That commented out part is where I had inserted the line for testing. I haven't 
tried it with a file, but the regex is, at least, accurate.

Hope this helps,

Tony

Tony Tambasco
Director, An Experiment with an Air Pump
Clarkson Theatre Co, April 2009
tonytambasco.com
email: [email protected]
cell: 716-450-8148


--- On Tue, 4/28/09, AJ ONeal <[email protected]> wrote:

> From: AJ ONeal <[email protected]>
> Subject: batch replace camelCase with _underscores_?
> To: [email protected]
> Date: Tuesday, April 28, 2009, 6:10 PM
> I've got a 7k-line javascript file that I'm trying
> to make readable and
> drastically reduce the size of.
> 
> Essentially I have several hundred lines like
> hideField(fieldName())
> 
> which I would like to turn into
> $('#field_name').hide()
> 
> and then I could delete several hundred lines of these
> hideField, showField,
> fieldAName(), fieldBName(), etc functions which are
> essentially just wasting
> space and overcomplicating the code.
> 
> I don't have enough regex-fu under my belt to have any
> reasonable idea of
> how to do this.
> Could any of you offer me some suggestions?
> 
> AJ ONeal


      

Reply via email to