[英]Using python re.sub, but it replace the start and end unexpected
I have this string a = "a:b/c\\"
and I want to replace : / \\
to _
together我有这个字符串a = "a:b/c\\"
我想将: / \\
替换为_
一起
This is my code这是我的代码
b = re.sub(r'[:/\\]*', '_', a)
However, the result is ''_a__b__c__''
and I think it should be a_b_c_
but this method replace the start and end together, how could I change this?但是,结果是''_a__b__c__''
我认为应该是a_b_c_
但是这种方法将开始和结束一起替换,我该如何更改呢?
a = "a:b/c\\"
b = re.sub(r'[:/\\]*', '_', a)
print(b)
You're using a character class []
which matches any single character from within that class.您使用的字符 class []
与该 class 中的任何单个字符匹配。 However this presents two issues in your particular scenario:但是,这在您的特定场景中提出了两个问题:
\\
你有一个要匹配的两个字符长的模式\\
*
, which means "zero or more matches" - at its core your pattern will now basically match on anything since this character class you've declared is now effectively optional.您已经用*
对其进行了量化,这意味着“零个或多个匹配项” - 在其核心,您的模式现在基本上可以匹配任何内容,因为您声明的这个字符 class 现在实际上是可选的。 The solution here is to (a) use a group and alternatives instead of a character class, and (b) eliminate the misused *
quantifier:这里的解决方案是 (a) 使用组和替代项而不是字符 class,以及 (b) 消除误用的*
量词:
import re
a = "a:b/c\\"
b = re.sub(r'(:|/|\\)', '_', a)
print(b) # 'a_b_c_'
Regex101 - this differs slightly because the tool itself does not respect the raw r''
string that Python uses to eliminate the need for escaping the backslash \
characters, regardless it illustrates fundamentally what's happening here. Regex101 - 这略有不同,因为该工具本身不尊重原始r''
字符串 Python 用于消除对 escaping 反斜杠\
字符的需要,无论它从根本上说明了这里发生的事情。
I have change re.sub(r'[:|/|\\]*', '_', a)
to re.sub(r'[:|/|\\]+', '_', a)
this problem solved, + means it need to exist 1 or more.我已将re.sub(r'[:|/|\\]*', '_', a)
更改为re.sub(r'[:|/|\\]+', '_', a)
这个问题解决了,+ 表示它需要存在 1 个或多个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.