Unix和Dos Lineendings的常规表达通缉 [英] regular expresson for Unix and Dos Lineendings wanted
问题描述
您好,我需要一个regularrexpression,它会修剪尾随空格。
使用unix行结尾时,它可以工作;
但不能使用Window(Dos) )CRLF''s:
Hello, I need a regularexpression, which trims trailing whitespaces.
While with unix line endings, it works;
but not with Window (Dos) CRLF''s:
import re
retrailingwhitespace = re.compile(''(? < = \S)[\ t] + $'',re.MULTILINE)
1)Windows r =" erewr \\\\\\\\\\\\ br /> newtext,n = retrailingwhitespace.subn('''',r)
n
1 newtext
''erewr \r\\\
afjdskl''
2)Unix r =" erewr \ nafjdskl"
newtext,n = retrailingwhitespace.subn('''',r)
n
2 newtext
''erewr\\\
fjdskl''
import re
retrailingwhitespace = re.compile(''(?<=\S)[ \t]+$'', re.MULTILINE)
1) Windows r="erewr \r\nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 1 newtext ''erewr \r\nafjdskl''
2) Unix r="erewr \nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 2 newtext ''erewr\nafjdskl''
谁可以帮助我(正则表达式,适用于这两种情况)。
提前谢谢!
-
Franz Steinhaeusler
Who can help me (regular expression, which works for both cases).
Thank you in advance!
--
Franz Steinhaeusler
推荐答案
'',re.MULTILINE)
1)Windows r =erewr \ rr \\ n \\ n \\ n \\ n \\ n \\ n \\ n> \\ n \\ n \\ n \\ n>
newtext,n = retrailingwhitespace.subn('''',r)
n
1 newtext
''erewr \\\\\\\\\\ br />
2)Unix r =" erewr \ nafjdskl"
newtext,n = retrailingwhitespace.subn('''',r)
n
2 newtext
''erewr\\\
afjdskl''
'', re.MULTILINE)
1) Windows r="erewr \r\nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 1 newtext ''erewr \r\nafjdskl''
2) Unix r="erewr \nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 2 newtext ''erewr\nafjdskl''
谁可以帮助我(正则表达式,有效对于这两种情况)。
提前谢谢!
-
Franz Steinhaeusler
Who can help me (regular expression, which works for both cases).
Thank you in advance!
--
Franz Steinhaeusler
Franz Steinhaeusler写道:
Franz Steinhaeusler wrote:
你好,我需要一个regularrexpression,它修剪尾随空格。
使用unix行结尾时,它可以工作;
但没有使用Window(Dos)CRLF':
Hello, I need a regularexpression, which trims trailing whitespaces.
While with unix line endings, it works;
but not with Window (Dos) CRLF''s:
import re
retrailingwhitespace = re.compile(''(?< = \S)[\ t] +
import re
retrailingwhitespace = re.compile(''(?<=\S)[ \t]+
'',re .MULTILINE)1)Windows r =erewr \\\\\\\\ 'erewr \r\\\
afjdskl''
2)Unix r =" erewr \ nbspfjdskl"
newtext,n = retrailingwhitespace.subn('''',r)
n 2 newtext
'', re.MULTILINE)
1) Windows r="erewr \r\nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 1 newtext ''erewr \r\nafjdskl''
2) Unix r="erewr \nafjdskl "
newtext, n = retrailingwhitespace.subn('''', r)
n 2 newtext
''erewr \ nbspfjdskl''
谁可以帮助我(正则表达式,适用于这两种情况)。
提前谢谢!
''erewr\nafjdskl''
Who can help me (regular expression, which works for both cases).
Thank you in advance!
为什么不使用字符串方法strip,rstrip和lstrip
why not use string methods strip, rstrip and lstrip
这篇关于Unix和Dos Lineendings的常规表达通缉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!