简体   繁体   English

如何在 python 中找到具有已知起点但未知终点的文本 substring

[英]How to find a substring of text with a known starting point but unknown ending point in python

I have a long string of text.我有一长串文字。 I want to condense that string at a certain point using a key word to indicate the start of my new string in Python.我想使用关键字在某个点压缩该字符串,以指示我在 Python 中的新字符串的开始。 For example, my string is:例如,我的字符串是:

"Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street." “你好,我叫约翰。我今年二十五岁。我住在纽约市。我在华尔街工作。” I want the text from "New York" to the end of the text ie I need code to pull the substring "New York City. I work on Wall Street."我想要从“纽约”到文本末尾的文本,即我需要代码来提取 substring“纽约市。我在华尔街工作。”

have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"

Any help would be much appreciated!任何帮助将非常感激!

I believe the best way to do this would be with regex:我相信最好的方法是使用正则表达式:

import re

have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"

key_phrase_begins = re.search(key_phrase, have).span()[0]
new_string = have[key_phrase_begins:]
print(new_string) # Outputs: 'New York City. I work on Wall Street.'

What this is doing is searching for your key_phrase, and the index position at which the key phrase begins within the string.这样做是搜索您的 key_phrase,以及关键字在字符串中开始的索引 position。 Then it is using indexing to create the new string from where the key_phrase begins in the original string.然后它使用索引从原始字符串中 key_phrase 开始的位置创建新字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 Python 中的起点和终点相同,范围将如何工作 - How would a range work if the starting point and ending points are the same in Python Python在椭圆上找到已知点的投影点 - Python find the projection point of a known point on an ellipse 如何创建一条知道起点、终点和距离的路线 - how to create a route knowing starting point, ending point and distance to travel Python:在Python中查找并删除以特定子字符串开头和结尾的字符串 - Python: Find and remove a string starting and ending with a specific substring in python 如何检查文件是否存在于以 substring 开头并以 substring 结尾的文件夹中 Python - How to check if a file exists in a folder starting with a substring and ending with a substring in Python 在python中查找并删除以特定子字符串开头和结尾的字符串 - Find and remove a string starting and ending with a specific substring in python 如何找出任何Python项目中代码执行的起点? - How to find out the starting point of code execution in any Python project? 如何到达迷宫的终点? - How to come to the ending point of a maze? 如何在 python 中找到拐点? - How to find inflection point in python? 如何在列表中找到开始和结束元素的单词索引? 蟒蛇 - How to find index of word starting and ending an element in a list? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM