0

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
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Miquel Torres
  • 63
  • 3
  • 10

2 Answers2

2

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"
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

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])
Jay Patel
  • 530
  • 1
  • 3
  • 14