i want to extract a part of text that begins for example with
"Hello" and ends with "goodbye"
Example:
Extract the sentence Hello i'm Gabi, :D goodbye
from:
asdasd dwref ADSADSADA Hello i'm Gabi :D goodbye asd asl sodjasdji asdoija
i want to extract a part of text that begins for example with
"Hello" and ends with "goodbye"
Example:
Extract the sentence Hello i'm Gabi, :D goodbye
from:
asdasd dwref ADSADSADA Hello i'm Gabi :D goodbye asd asl sodjasdji asdoija
You can use a very basic regex:
(A demo and explanation on how it works: https://regex101.com/r/bO0rL7/2)
import re
string = "asdasd dwref ADSADSADA Hello i'm Gabi :D goodbye asd asl sodjasdji asdoija"
match = re.findall(r'hello .+ goodbye', string, flags=re.IGNORECASE)
if match:
print(match[0])
>> "Hello i'm Gabi :D goodbye"
Unless you want to implement NLP, and are not familiar with regex a simple way to do it will be as follows:
import sys
s = "asdasd dwref ADSADSADA Hello i'm Gabi :D goodbye asd asl sodjasdji asdoija"
hello = s.find("Hello")
goodbye = s.find("goodbye")
if hello == -1 or goodbye == -1:
print("Not found")
sys.exit(0)
goodbye += len("goodbye")
print(s[hello:goodbye])