regular expresson for Unix and Dos Lineendings wanted

Franz Steinhaeusler franz.steinhaeusler at gmx.at
Thu Feb 23 10:12:10 EST 2006


On Thu, 23 Feb 2006 15:59:54 +0100, Franz Steinhaeusler
<franz.steinhaeusler at gmx.at> wrote:

>I need it for a file, whose line endings I don't know.
>
>I wrote for DrPython this script:
>(using styled text control and wxPython) and this works,
>but I'm looking for a shorter way:

ah, sorry, I try to make this more clear again:

(DrDocument is instance of a styled text control)

import re
import string

def GetEndOfLineCharacter():
    emode = DrDocument.GetEOLMode()
    if emode == wx.stc.STC_EOL_CR:
      return '\r'
    elif emode == wx.stc.STC_EOL_CRLF:
      return '\r\n'
    return '\n'

text = DrDocument.GetText()

eol = GetEndOfLineCharacter()
regex = re.compile('\s+' + eol, re.MULTILINE)

lines = text.split(eol)
    
new_lines = []
for line in lines:
  result = regex.search(line + eol)
  if result != None:
    end = result.start()
    new_lines.append (line [:end])
  else:
    new_lines.append(line)
    
    
newtext = string.join(new_lines, eol)
DrDocument.SetText(newtext)

-- 
Franz Steinhaeusler



More information about the Python-list mailing list