[nzlug] A little help with some find and replace?

Cliff Pratt enkidu at cliffp.com
Fri Mar 28 18:36:51 NZST 2008


What's unreadable about "s/\[(\w+) (.*)\]/[\1 \L\2\E]/g"??

Cheers,

Cliff

Greg Fawcett wrote:
> I think I win on readability, but I have to submit to you on conciseness.
> 
> On 28/03/2008, Simon Green <simon at simongreen.net> wrote:
>> On 28/03/2008, Greg Fawcett <greg at vig.co.nz> wrote:
>>> You could spend hours getting the regex right, or you could use a little
>>>  python magic...
>>>
>>>  inFile=file('input.txt','r')
>>>  outFile=file('output.txt','w')
>>>  lineCount=0
>>>  for line in inFile:
>>>     lineCount+=1
>>>     if line[0]=='[':
>>>         spacePos=line.find(' ')
>>>         if spacePos!=-1:
>>>             outFile.write(line[:spacePos]+line[spacePos:].lower())
>>>         else:
>>>             print "Line %d doesn't contain a space"%lineCount
>>>     else:
>>>         print "Line %d doesn't start with '['"%lineCount
>>>  inFile.close()
>>>  outFile.close()
>>>
>>>  This will convert infile.txt to outfile.txt, with some rudimentary
>> error
>>>  checking.
>> You know you've been using Perl too long when
>>
>> s/\[(\w+) (.*)\]/[\1 \L\2\E]/g
>>
>> makes more sense that you python code. :P
>>




More information about the NZLUG mailing list