我有一根绳子。如何删除某个字符后的所有文本?(在这种情况下……) 之后的文字将…这就是为什么我想删除某一个字符之后的所有字符。


当前回答

另一种使用re的简单方法是

import re, clr

text = 'some string... this part will be removed.'

text= re.search(r'(\A.*)\.\.\..+',url,re.DOTALL|re.IGNORECASE).group(1)

// text = some string

其他回答

在你的分离器上最多分裂一次,然后取出第一块:

sep = '...'
stripped = text.split(sep, 1)[0]

你没说如果分隔器不存在会发生什么。在这种情况下,这个和Alex的解决方案都会返回整个字符串。

find方法将返回字符串中的字符位置。然后,如果你想从角色中删除所有东西,这样做:

mystring = "123⋯567"
mystring[ 0 : mystring.index("⋯")]

>> '123'

如果你想保留字符,在字符位置上加1。

这是在python 3.7工作到我 在我的情况下,我需要删除后点在我的字符串可变费用

费用= 45.05 Split_string = fees.split(".", 1)

[0] . Substring = split_string

打印(字符串)

没有正则表达式(我假设这是你想要的):

def remafterellipsis(text):
  where_ellipsis = text.find('...')
  if where_ellipsis == -1:
    return text
  return text[:where_ellipsis + 3]

或者,使用正则表达式:

import re

def remwithre(text, there=re.compile(re.escape('...')+'.*')):
  return there.sub('', text)

另一种使用re的简单方法是

import re, clr

text = 'some string... this part will be removed.'

text= re.search(r'(\A.*)\.\.\..+',url,re.DOTALL|re.IGNORECASE).group(1)

// text = some string