从特定点删除文本

2024-09-29 00:20:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想删除与python中特定点不同的文本部分

例:

lksalksapointdlksla
删除“点”之前的所有内容
pointdlksla

kljkghglpointsdfasfsd
删除“点”之前的所有内容
点DFSFSD

这可能吗


Tags: 文本内容dfsfsdkljkghglpointsdfasfsdpointdlkslalksalksapointdlksla
3条回答

您可以使用正则表达式:

import re

regex = r"(?P<before>.*)(point)(?P<after>.*)"

test_str = "lksalksapointdlksla"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

然后,您可以使用beforeafter组执行您喜欢的操作

你可以这样做:

text = "kljkghglpointsdfasfsd"
text = text.split("point")[1]
print(text)

我将使用.find()和字符串切片:

s = 'lksalksapointdlksla'
s = s[s.find('point'):]
print(s)

相关问题 更多 >