String Manipulation Help!

Kirk McDonald mooquack at suad.org
Sat Jan 28 16:11:30 EST 2006


Dave wrote:
> OK, I'm stumped.
> 
> I'm trying to find newline characters (\n, specifically) that are NOT
> in comments.
> 
> So, for example (where "<-" = a newline character):
> ==========================================
> 1: <-
> 2: /*<-
> 3: ----------------------<-
> 4:     comment<-
> 5: ----------------------<-
> 6: */<-
> 7: <-
> 8: CODE CODE CODE<-
> 9: <-
> ==========================================

[snip]

Well, I'm sure there is some regex that'll do it, but here's a stupid 
iterative solution:

def newlines(s):
     nl = []
     inComment = False
     for i in xrange(len(s)):
         if s[i:i+2] == '/*':
             inComment = True
         if s[i:i+2] == '*/':
             inComment = False
         if inComment: continue
         if s[i] == '\n':
             nl.append(i)
     return tuple(nl)

Your example returns:
(0, 64, 65, 80, 81)

This probably isn't as fast as a regex, but at least it works.

-Kirk McDonald



More information about the Python-list mailing list