1

I am very new to both Python and regular expressions, so bear with me. I have some text that looks like this:

Change 421387 on 2011/09/20 by [email protected]

    Some random text    including line breaks

Change 421388 on 2011/09/20 by [email protected]

    Some other random text  including line breaks

Now, I want to use python along with a regular expression to split this into a tuple. In the end I want the tuple to contain two elements.

Element 0:

Change 421387 on 2011/09/20 by [email protected]

    Some random text    including line breaks

Element 1:

Change 421388 on 2011/09/20 by [email protected]

    Some other random text  including line breaks

I realize that I can use regex to recognize the pattern formed by:

  • the word "Change"
  • a space
  • some digits
  • some text
  • a date in the form ####/##/##
  • some text
  • @
  • some text
  • line break

I know it could be broken down further, but I think recognizing these things is good enough for my purposes.

Once I come up with a regular expression for that pattern, how can I use it to split the string into a tuple of strings?

1
  • 2
    "Bare with me" is an invitation to undress. Commented Sep 20, 2011 at 22:36

1 Answer 1

4

With a lookahead assertion.

>>> re.split(r'(?=\s+Change \d+ on \d{4})\s+', '''    Change 421387 on 2011/09/20 by [email protected]
...     Some random text including line breaks
...     Change 421388 on 2011/09/20 by [email protected]
...     Some other random text including line breaks''')
['', 'Change 421387 on 2011/09/20 by [email protected]\n    Some random text including line breaks', 'Change 421388 on 2011/09/20 by [email protected]\n    Some other random text including line breaks']
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! It would have taken me forever to figure this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.