Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How should I extract the string from paragraph using Python?

The input:
I got the email from your company for the refund of the ticket, here is my identity
Flight no: XX 888
Route: USA-THA
Date: Sunday, 2 December 2010
Name: Richard Kylie

and the output is 

```lang-none
Name: "Richard Kylie" 
flight_no: "XX 888" 
date:"Sunday 2 December 2010"
route:"USA-THA"
```


What I have tried:

```python
import re

def extract_mentions(text):
    regex = ("Flight no:\s(\w+)\s(\w+)")
    return re.findall(regex, str(text))
string :'I got the email from your company for the refund of the ticket, here is my identity
Flight no: XX 888
Route: USA-THA
Date: Sunday, 2 December 2010
Name: Richard'
mentions = extract_mentions(string)
print('Flight:', mentions)
```
and the output is

```python
Flight: [('XX', '888')]
```
and my desired output is:

```python
Name: "Richard Kylie" 
flight_no: "XX 888" 
date:"Sunday 2 December 2010"
route:"USA-THA"
```
Posted
Updated 3-Oct-22 22:06pm

1 solution

You don't really need a RegEx for this. Just read the text and look for lines beginning with the relevant keywords: [ "Flight no", "Route", "Date", "Name" ].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900