1

I have the following code that matches almost everything I need it to.

import re
rx_sequence=re.compile(r"^(\d+:\s*\(\*\s*T.+)(?:\n?)((?:(?:\n|\r\n?).+)+)", re.MULTILINE)
text="""
2:(* Test #1 :: trj6tkjtkjty7ry7kyrukjkuy*)

  rtjtyjtryjtrkjyryukryukrkuy
test3test3test3test3test3test3+1;

3:(* Test3:: test3test3test3test3test3 *)

  Twwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwing test69test69';
tyjtdyjtdyjnrdtyntrdyntyntyn

69:(* Ttest69test69:test69test69 *)
  (*test69test69test69test69test69test69test69test69test69test6940: (* Finish Test case *)

    bTestDone := TRUE;

  40: (* Finish Test case *)

    bTestDone := TRUE;


  END_CASE;
  END_CASE;



(**********test10test10test10test10test10**************************)
10: (* Test10test10test10test10test10test10test10test10test10test10test10 *)

  test10[test10] := 'test10'; 
  (* petest10test10test10test10test10test10test10test10 *)  
  btest10test10e := TRUE;

  (* Run Sih';io0;'ioh;ui;oi;io;io;anageState OF

"""
for match in rx_sequence.finditer(text):
    title, sequence = match.groups()
    title = title.strip()
    print ("Title:\n",title)
    print ("\nSequence===========================================================================================:",sequence)
    print ("\n\n")

My regex isn't matching some of the body of case 69, and the last line of case 10. I've been trying to come up with a regex that matches everything but no luck... I'm not sure what to do next.

1

1 Answer 1

1

You could update the second part of your pattern to match the following lines that do not start with the pattern that matching at the beginning.

^(\d+:\s*\(\*\s*T.+)(?:\r?\n(?!^\d+:\s*\(\*\s*T).*)*

About the second part (?:\r?\n(?!^\d+:\s*\(\*\s*T).*)*

  • (?: Non capturing group
    • \r?\n
    • (?!^\d+:\s*\(\*\s*T) If what is on the right is not the pattern that indicated the beginning
    • .* Match any char 0+ times except a newline
  • )* Close non capturing group

Regex demo

You can also wrap the second part in a capturing group if you want to have 2 groups in total.

regex demo with 2 capturing groups

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that captures all the groups, thank you. However if i were to add another case statement that begins with a different letter, why doesnt that get matched? regex101.com/r/aOFzg9/2
@Real-Ragnar You have to add the charater class [TF] to the end as well regex101.com/r/2su7BY/1

Your Answer

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