[英]Python Regular Expression String Substitution Questions
I have the text "languageType": "en"
, and I want to replace it with en
.我有文本"languageType": "en"
,我想用en
替换它。 How do I do it using replace?我如何使用替换来做到这一点? I've tried this so far so it's not working到目前为止我已经尝试过了,所以它不起作用
lang = s.split(":")[0].replace("[\"']", "", -1).strip()
print(lang)
But I keep getting "en"
但我不断收到"en"
The replace method doesn't accept a regular expression, but rather a string. replace 方法不接受正则表达式,而是字符串。 To replace with a regex use re.sub()
.要使用正则表达式替换,请使用re.sub()
。 See How to input a regex in string.replace?请参阅如何在 string.replace 中输入正则表达式?
I've made the same mistake before, myself.我自己也犯过同样的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.