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

Greg Fawcett greg at vig.co.nz
Fri Mar 28 10:55:55 NZST 2008


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
>
>   -- simon
>
> _______________________________________________
> NZLUG mailing list NZLUG at linux.net.nz
> http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug
>



-- 
Phone: +64 3 409 8165
Mobile: +64 21 333 291
Fax: +64 3 974 6810
Web: www.vig.co.nz


More information about the NZLUG mailing list