开发者

Some help with a regular expression in Python

开发者 https://www.devze.com 2023-04-06 11:52 出处:网络
I am very new to both Python and regular expressions, so bear with me. I have some text that looks like this:

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 person@domain.com

    Some random text    including line breaks

Change 421388 on 2011/09/20 by person2@domain.com

    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 person@domain.com

    Some random text    including line breaks

Element 1:

Change 421388 on 2011/09/20 by person2@domain.com

    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 reg开发者_JAVA百科ular expression for that pattern, how can I use it to split the string into a tuple of strings?


With a lookahead assertion.

>>> re.split(r'(?=\s+Change \d+ on \d{4})\s+', '''    Change 421387 on 2011/09/20 by person@domain.com
...     Some random text including line breaks
...     Change 421388 on 2011/09/20 by person2@domain.com
...     Some other random text including line breaks''')
['', 'Change 421387 on 2011/09/20 by person@domain.com\n    Some random text including line breaks', 'Change 421388 on 2011/09/20 by person2@domain.com\n    Some other random text including line breaks']
0

精彩评论

暂无评论...
验证码 换一张
取 消