I am trying to parse text to extract desired strings. I am missing something in the regex, can someone help me figure out what is the issue here?
This is my script:
import re
a = """
block1
#(/*AUTOINSTPARAM*/
// Parameters
.THREE (3), // comment
.TWO (2), // comment
.ONE (1)) // comment
inst1
(/*AUTOINST*/
// extra
// output
block2
#(/*AUTOINSTPARAM*/
// Parameters
.THREE (3), // comment
.TWO (2), // comment
.ONE (1)) // comment
inst2
(/*AUTOINST*/
// extra
// output
"""
op = re.findall(r'(\w+)\s*(#\(.*\))?.*?(\w+)\s*\(', a, re.MULTILINE|re.DOTALL)
for i in op:
print(i[0],i[2])
This is the output:
('block1', 'inst2')
Expected output:
('block1', 'inst1')
('block2', 'inst2')
Updated: Trying to test following input for the same regex as accepted answer:
import re
a = """
except_check
#(
.a (m),
.b (w),
.c (x),
.d (1),
.e (1)
)
data_check
(// Outputs
abc
#(
.a (b::c)
)
mask
(/*AUTOINST*/
"""
op = re.findall(r'^\s*(\w+)\s*$\n(?:^\s*[#/.].*$\n)*^\s*(\w+)\s*\(', a, re.MULTILINE)
for i in op:
print(i)
It did not return anything. It should have returned following:
('except_check', 'data_check')
('abc', 'mask')