如何替换字符串中的逗号

时间:2021-01-10 16:49:32

I want to remove all commas from a string using regular expressions, I want letters, periods and numbers to remain just commas

我想使用正则表达式从字符串中删除所有逗号,我希望字母,句点和数字保持逗号

2 个解决方案

#1


26  

Why a regex?

什么是正则表达式?

mystring = mystring.replace(",", "")

is enough.

足够。

Of course, if you insist:

当然,如果你坚持:

mystring = re.sub(",", "", mystring)

but that's probably an order of magnitude slower.

但这可能要慢一个数量级。

#2


7  

You don't need RE for that trivial operation. just use replace() on string:

您不需要RE进行那种微不足道的操作。只需对字符串使用replace():

a="123,asd,wer"
a.replace(",", "")

#1


26  

Why a regex?

什么是正则表达式?

mystring = mystring.replace(",", "")

is enough.

足够。

Of course, if you insist:

当然,如果你坚持:

mystring = re.sub(",", "", mystring)

but that's probably an order of magnitude slower.

但这可能要慢一个数量级。

#2


7  

You don't need RE for that trivial operation. just use replace() on string:

您不需要RE进行那种微不足道的操作。只需对字符串使用replace():

a="123,asd,wer"
a.replace(",", "")