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

Simon Green simon at simongreen.net
Fri Mar 28 10:22:41 NZST 2008


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

  -- simon



More information about the NZLUG mailing list