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
1
  • What have you already tried? Commented Jun 18, 2016 at 16:05

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

3 Comments

really? is this works?
@AvinashRaj Yes. I've updated my answer, but it worked as well.
at first, I saw 2 goodbye's in ur regex string..
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])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.