简体   繁体   English

为什么我不能在python中用正则表达式替换目标字符串?

[英]Why can't I replace target string with regular expression in python?

I want to replace the \\n\\t+ in the string "2005-03-08\\n\\t\\t\\t\\t\\t10派3元(含税)\\n\\t\\t\\t\\t\\t" , why can't I get it to work? 我想,以取代\\n\\t+字符串中"2005-03-08\\n\\t\\t\\t\\t\\t10派3元(含税)\\n\\t\\t\\t\\t\\t" ,为什么我不能使它正常工作?

str1="2005-03-08\n\t\t\t\t\t10派3元(含税)\n\t\t\t\t\t"
str2=str1.replace("\n\t+","")
str2
'2005-03-08\n\t\t\t\t\t10派3元(含税)\n\t\t\t\t\t'

Why I can't get 2005-03-0810派3元(含税) as the result? 为什么我不能得到2005-03-0810派3元(含税)的结果?

Your code isn't doing a regular expression replacement, but rather it tries (unsuccessfully) to use the built in str.replace method. 您的代码没有执行正则表达式替换,而是尝试(未成功)使用内置的str.replace方法。 That doesn't work, as the semantics are not right. 这不起作用,因为语义不正确。

There are two reasonable fixes: 有两个合理的解决方法:

  1. You can stick with string replacement, and simply use the right syntax (but note that this replaces all tabs, not only ones that follow newlines): 您可以坚持使用字符串替换,并且只需使用正确的语法即可(但请注意,这将替换所有选项卡,不仅替换换行符的选项卡):

     str2 = str1.replace("\\n", "").replace("\\t", "") 
  2. You can import the re module and do your intended replacement: 您可以导入re模块并进行预期的替换:

     import re str2 = re.sub(r"\\n\\t+", "", str1) 

Well your main reason is because str2 is looking for '\\n\\t+' , which is not found in the statement. 好吧,您的主要原因是因为str2正在寻找'\\n\\t+' ,这在语句中找不到。 And also, your ideal output won't be like that because it is looking for removing all of the \\n\\t s, but your replace() only looks for the ones that come directly after a \\n . 而且,理想的输出将不是这样,因为它正在寻找删除所有 \\n\\t ,但是您的replace()只寻找直接在\\n之后的输出。 Try this code: 试试这个代码:

>>> str1="2005-03-08\n\t\t\t\t\t10派3元(含税)\n\t\t\t\t\t"
>>> ideal = "2005-03-0810派3元(含税)" #Just to check if they are the same
>>> str2 = str1.replace('\n', '').replace('\t', '')
>>> str2
'2005-03-0810\xe6\xb4\xbe3\xe5\x85\x83(\xe5\x90\xab\xe7\xa8\x8e)' #The encoded statement
>>> print str2
2005-03-0810派3元(含税)
>>> str2==ideal
True
>>> 

You could do 你可以做

str2 = ''.join(s.strip() for s in str1.splitlines())

(although this will also remove leading and trailing spaces). (尽管这也会删除前导和尾随空格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM