查找子字符串忽略返回

2024-09-30 03:26:12 发布

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

如何在忽略返回的同时从较大字符串的开头删除(子字符串)?例如,子字符串可能如下所示:

mysqldump --host="ssh.example.com" --user="jack" --password="3kg%39$(*kdsk#"

较大的字符串将如下所示(注意开头的子字符串和整个过程中的一些返回):

mysqldump --host="ssh.examp
le.com" --user="jack" --password="3
kg%39$(*kdsk#"
-- more stuff that needs to remain --
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Qua tu etiam inprudens
utebare non numquam. Sed haec quidem

如何从较大的字符串中查找并删除子字符串,使结果如下所示:

-- more stuff that needs to remain --
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Qua tu etiam inprudens
utebare non numquam. Sed haec quidem

从开头删除子字符串


Tags: to字符串comhostthatmorepasswordssh
1条回答
网友
1楼 · 发布于 2024-09-30 03:26:12

旧答案:

希望我能正确理解你的规格。这就是你要问的吗

>>> s = '''mysqldump  host="ssh.examp
... le.com"  user="jack"  password="3
... kg%39$(*kdsk#"'''
>>> s = s.replace('\n','').partition(' ')[-1]
>>> s
' host="ssh.example.com"  user="jack"  password="3kg%39$(*kdsk#"'

最新答案:

I will always know exactly what the first string looks like and how long it is. The second string will have that first string at the beginning, but with returns randomly scattered throughout. As for the second string, that could look like anything after the end of the first string.

那样的话,我建议这样做

>>> import re
>>> s = 'mysqldump  host="ssh.examp\nle.com"  user="jack"  passw\nwo\nr\n\nd\n=\n"3\nkg%39$(*kdsk#"this is the stuff\nthat"needs"to remain!'
>>> s[re.search(r'd\n*=\n*"[^"]*"', s).end():]
'this is the stuff\nthat"needs"to remain!'

相关问题 更多 >

    热门问题