处理反斜杠转义字符的好方法是什么?
我有一个以下格式的字符串;
s="part1,part2,part3,part4"
我只需调用 s.split(",")
命令即可将字符串分割成多个部分。
现在的问题是,如果字符串中存在反斜杠转义逗号怎么办?假设我有以下字符串,
s="part1,part2,pa\\,rt3,part4"
我希望能够得到 ["part1","part2","pa,rt3","part4"]
结果。
我最初的想法是,将 \,
替换为不存在的字符串,然后使用 split 命令分割该字符串,并将不存在的字符串替换为逗号。
你能想出更好的方法来处理这个问题吗?
I have a string in the following format;
s="part1,part2,part3,part4"
I can split the string into pieces by just invoking the s.split(",")
command.
Now, the question is what if I have a backslash escaped comma in the string? Assuming I have the following string,
s="part1,part2,pa\\,rt3,part4"
I'd like to be able to get ["part1","part2","pa,rt3","part4"]
as the result.
What I initially thought was to replace the \,
with a non-existent string, then split the string by using the split command and replace the non-existent string with a comma.
Can you think of a better way to deal with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用不存在的字符串替换它是一个不错的选择。
否则,您可以使用带有负向后查找的正则表达式,如下所示:
Replacing it with a non-existing string is a nice option.
And otherwise, you could use a regular expression with a negative lookbehind like this:
csv 模块也可以处理此问题
:
The csv module can handle this as well:
Output
顺便说一句,“\”不是转义字符 为 ',' 逗号。所以你的字符串将有一个合法的单词“\”。如果您特别希望 \, 成为单词的一部分,那么基于正则表达式的解决方案对我来说看起来不错。
BTW, '\' is not an escape character for ',' comma. So your string would have have a legal word with '\'. If you specially want the \, to be part of the word, then a regex based solutions looks good to me.