I have a string like:
我有一个字符串:
text1 = 'python...is...fun...'
I want to replace the multiple '.'s to one '.' only when they are at the end of the string, i want the output to be:
我想将多个'。'替换为''。'只有当它们在字符串的末尾时,我希望输出为:
python...is...fun.
So when there is only one '.' at the end of the string, then it won't be replaced
所以当只有一个'。'时在字符串的末尾,它将不会被替换
text2 = 'python...is...fun.'
and the output is just the same as text2
输出与text2相同
My regex is like this:
我的正则表达式是这样的:
text = re.sub(r'(.*)\.{2,}$', r'\1.', text)
which i want to match any string then {2 to n} of '.' at the end of the string, but the output is:
我希望匹配任何字符串然后{2到n}的'。'在字符串的末尾,但输出是:
python...is...fun..
any ideas how to do this? Thx in advance!
任何想法如何做到这一点? Thx提前!
4 个解决方案
#1
2
You are making it a bit complex, you can easily do it by using regex as \.+$
and replace the regex pattern with single .
character.
你正在使它变得有点复杂,你可以通过使用正则表达式作为\。+ $并用单个替换正则表达式来轻松实现。字符。
>>> text1 = 'python...is...fun...'
>>> new_text = re.sub(r"\.+$", ".", text1)
>>> 'python...is...fun.'
You may extend this regex further to handle the cases with input such as ...
only, etc but the main concept was that there is no need to counting the number of .
, as you have done in your answer.
您可以进一步扩展此正则表达式以处理具有输入的情况,例如......仅等,但主要概念是不需要计算。的数量,正如您在答案中所做的那样。
#2
0
import re
print(re.sub(r'\.{2,}$', '.', 'I...love...python...'))
As simple as that. Note that you need to escape the .
because otherwise, it means whichever char except \n
.
就如此容易。请注意,你需要逃避。因为否则,它意味着除了\ n之外的任何一个字符。
#3
0
I want to replace the multiple '.'s to one '.' only when they are at the end of the string
我想将多个'。'替换为''。'只有当它们在字符串的末尾时
For such simple case it's easier to substitute without importing re
module, checking the value of the last 3 characters:
对于这种简单的情况,在不导入re模块的情况下更容易替换,检查最后3个字符的值:
text1 = 'python...is...fun...'
text1 = text1[:-2] if text1[-3:] == '...' else text1
print(text1)
The output:
python...is...fun.
#4
0
Just look for the string ending with three periods, and replace them with a single one.
只需查找以三个句点结尾的字符串,并将其替换为单个句点。
import re
x = "foo...bar...quux..."
print(re.sub('\.{2,}$', '.', x))
// foo...bar...quux.
#1
2
You are making it a bit complex, you can easily do it by using regex as \.+$
and replace the regex pattern with single .
character.
你正在使它变得有点复杂,你可以通过使用正则表达式作为\。+ $并用单个替换正则表达式来轻松实现。字符。
>>> text1 = 'python...is...fun...'
>>> new_text = re.sub(r"\.+$", ".", text1)
>>> 'python...is...fun.'
You may extend this regex further to handle the cases with input such as ...
only, etc but the main concept was that there is no need to counting the number of .
, as you have done in your answer.
您可以进一步扩展此正则表达式以处理具有输入的情况,例如......仅等,但主要概念是不需要计算。的数量,正如您在答案中所做的那样。
#2
0
import re
print(re.sub(r'\.{2,}$', '.', 'I...love...python...'))
As simple as that. Note that you need to escape the .
because otherwise, it means whichever char except \n
.
就如此容易。请注意,你需要逃避。因为否则,它意味着除了\ n之外的任何一个字符。
#3
0
I want to replace the multiple '.'s to one '.' only when they are at the end of the string
我想将多个'。'替换为''。'只有当它们在字符串的末尾时
For such simple case it's easier to substitute without importing re
module, checking the value of the last 3 characters:
对于这种简单的情况,在不导入re模块的情况下更容易替换,检查最后3个字符的值:
text1 = 'python...is...fun...'
text1 = text1[:-2] if text1[-3:] == '...' else text1
print(text1)
The output:
python...is...fun.
#4
0
Just look for the string ending with three periods, and replace them with a single one.
只需查找以三个句点结尾的字符串,并将其替换为单个句点。
import re
x = "foo...bar...quux..."
print(re.sub('\.{2,}$', '.', x))
// foo...bar...quux.