Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read re.split expression
#1
Hi,

I have this expression
text_list = re.split('\n\n\n', text)
I read already the Slipt() method that expression confuses me. the output of it it's a list []:
Output:
['Good Morning\n\nthis is testing test.', 'Vriendelike groete/ Kind regards\n\nJoe Fraser\n\nDirect tel: 001 555 3651 | Email:\[email protected]', '', '\nFrom: [email protected]\n\nSent: 28 March 2019 11:00 AM\nTo: King Brand;[email protected];\nSubject: Notification of Creation', '\n\nDear Boss', 'How are you\ntoday\n\nYour number\n\nWhen you contacted us: 2019-03-2810:15 AM\u2009\n\nSrequesting\n\nBranch Agency: Meme\u2009\n\nBranch Agency Code: 0329271\u2009\n\nThank you for contacting us\n\'Kind regards\n\nAgriculture Contact Centre \u2009', 'E-Mail: [email protected]\u2009']
My confusion is that '\n\n\n'

Please help

Here's the full code:

import re
from bs4 import BeautifulSoup

html = '''
Good Morning

We need your input please.


Vriendelike groete/ Kind regards

Badu Thusong

Direct tel: 021 974 7313 | Email:
[email protected]






From: [email protected]]

Sent: 28 March 2019 11:00 AM
To: King Budo;[email protected];
Subject: Notification of Creation




Dear Boss


How are you

today

Your number

Branch Agency: Meme

Branch Agency Code: 0329271

Thank you for contacting us


Kind regards

Agriculture Contact Centre


E-Mail: [email protected]
'''

# print(html)

text_list = re.split('\n\n\n', html)
print(text_list)
Output:
Output:
['\nGood Morning\n\nWe need your input please.', 'Vriendelike groete/ Kind regards\n\nBadu Thusong\n\nDirect tel: 021 974 7313 | Email:\[email protected]', '', '\nFrom: [email protected]]\n\nSent: 28 March 2019 11:00 AM\nTo: King Budo;[email protected];\nSubject: Notification of Creation', '\n\nDear Boss', 'How are you\n\ntoday\n\nYour number\n\nBranch Agency: Meme\n\nBranch Agency Code: 0329271\n\nThank you for contacting us', 'Kind regards\n\nAgriculture Contact Centre', 'E-Mail: [email protected]\n']
I'm really not sure what does those new lines do, more especially that it's three of them in the expression.
Reply
#2
re.split(r'\n\n\n', html) -> Split the string html in elements everytime \n\n\n sequence is found. It returns a list of all these elements.
Note that it doesn't split \n nor \n\n...

As an example try re.split('\n', html). It will split all the \n in html.
Reply
#3
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using the reg expression split to get the words vlrk 2 1,944 May-10-2020, 06:44 AM
Last Post: bowlofred
  Pass results of expression to another expression cmdr_eggplant 2 2,278 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  [split] AttributeError: 'str' object has no attribute 'read' laxmipython 2 4,185 Feb-14-2020, 01:15 PM
Last Post: laxmipython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020