0

I have an expression in regex, as follow:

r"(?:AND|OR|SUB|ADD)\([^()]*\)(?:\]\[|\[|)(|\s\[|\s)COIL\(Seq\[\d+]\.Bool\[\d+]\.\d+"

Usually I am using it to capturing from a sentence like that:

AND(Seq[1].Mat)AND(Type_G012.WithData)[COIL(Seq[1].Bool[93].11),XIC(Seq[1].exp)RES(Seq[1].ita)]

So I want to extract the last "AND" or "OR" or "SUB"... also with Seq[1].Bool[93].11. After that I am doing an additional extraction. It was working with no problems with almost everything. The problem is that I have some patterns like that.

AND(Seq[1].Mat)AND(Type_G014.WithData)AND(Type_G015.WithData)[SET(Seq[1].WaitStep)COIL(Seq[1].Seq[93].10),AND(Seq[1].exp)RES(Seq[1].ita)]

Then I am not capturing the last AND, OR, SUB, etc. Because now I have the SET instruction in the middle of the AND and the COIL. So I want to exclude anything diferent of AND|OR|SUB|ADD Because I would like to extract from the last sentence as follow:

AND(Type_G015.WithData)[SET(Seq[1].Wait)COIL(Seq[1].Seq[93].10

Then is the last AND before the COIL. If something could help me I am testing several things and I am messing it.

Thanks.

Jmm86
  • 103
  • 8

2 Answers2

1

To match both parts, you might use

(?:AND|OR|SUB|ADD)\([^()]+\)(?:\[SET\([^()]+\))?\[?COIL\(Seq\[\d+\]\.(?:Seq|Bool)\[\d+\]\.\d+

In parts

  • (?:AND|OR|SUB|ADD) Match 1 of the alternatives
  • \([^()]+\) Match from an opening till closing parenthesis
  • (?:\[SET\([^()]+\))? Optionally match [SET and from opening till closing parenthesis
  • \[?COIL\(Seq\[\d+\]\. Match Optional [ and COIL(Seq[ 1+ digits and ].
  • (?:Seq|Bool) Match either Seq or Bool
  • \[\d+\]\.\d+ Match [ 1+ digits and ]. followed by 1+ digits

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Thanks.

Now I have something like that:

AND(Type_G015.WithData)[SET(Seq[1].Wait)COIL(Seq[1].Seq[93].10

How Can I extract just AND(Type_G015.WithData). Because my idea was as follow:

(?:AND|OR|SUB|ADD)\((.*)\) But now I am extracting until the last parenthesis but I would like to extract everythin but only until the first closing parenthesis, just:

AND(Type_G015.WithData)

Between parenthesis we could be whatever less more parenthesis.

Jmm86
  • 103
  • 8
  • I found a solution for that. Checking this thread. https://stackoverflow.com/questions/5357460/python-regex-matching-a-parenthesis-within-parenthesis – Jmm86 May 18 '20 at 15:26