Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re match a pattern

Tags:

python

regex

I have some output from a CAS and I want to split up the stuff into three, here is some sample output:

' 1+2;\r\n\r(%o2)                                  3\r\n(%i3) '
'?\r\n\r\n\rpos;\r\n\r(%o1)                                  0\r\n(%i2) '

I'd like to separate the output into three parts:

  1. The part from the beginning of the string to the ';' semi-colon.
  2. The part from after the semi-colon to just before the final \r\n\(%i\d+\)
  3. The final part to be by itself ie.\r\n\(%i\d+\) to always be alone in the final part.

how would I separate them? I'm having trouble creating the code to do that.

EDIT: I'd like the semicolon to be retained even after separating the sections.

like image 834
mike Avatar asked May 21 '26 22:05

mike


1 Answers

This should do what you have requested:

re.findall('^([^;]+);(.*)(\r\n\(%i\d+\).+)$', text, re.S)

To include the semicolon in the first group, just add it to the grouping parenthesis:

re.findall('^([^;]+;)(.*)(\r\n\(%i\d+\).+)$', text, re.S)
like image 75
woemler Avatar answered May 23 '26 12:05

woemler