Find all occurrences of a string except if found within another pattern
I have blocks of text like below where I am looking to find all occurrences of; data ...; ... run; where ... can be any type of string pattern. I want to only find occurences of this where the pattern is not within C style comments or if it is wrapped in another pattern like below. I want to find all occurrences of; data foo; set bar; run; but not %macro x(); data foo; set bar; run; %mend; or /* data foo;*/ /* set bar;*/ /* run;*/ I have the following function which will exclude the pattern when wrapped in a comment or %macro ... %mend however it is only returning the last match and not each occurrence. How can I adjust this to return every match as a list of lists with one list per block? Thanks in advance. s = """ /** * #file * #brief Description of the program */ /** * #macro xyz * #brief Description of the Macro */ %macro xyz(); data foo_nomatch; set bar; run; %mend; /** * #data foo_matchme * #brief Description of the DataStep */ data foo_matchme; set bar; run; # Should Not Match /** * data foo_nomatch2; * set bar; * run; */ /** * #datastep: foo2 * #brief: This is a description. */ # Should match as a 2nd match data foo_matchme2; set bar; run; """ def datastep(s): t1 = 'data' t2 = 'run;' t3 = ';' e1 = re.escape('/**') e2 = re.escape('*/') e3 = re.escape('%macro') e4 = re.escape('%mend') return re.findall('%s.*%s|%s.*%s|(%s.*?%s)' %(e1,e2,e3,e4,t1,t2),s,re.DOTALL|re.IGNORECASE) print(datastep(s))
Make the .*-part of the skip-subregexes non-greedy, i.e., change '%s.*%s|%s.*%s|(%s.*?%s)' to '%s.*?%s|%s.*?%s|(%s.*?%s)'. Demo: for match in datastep(s): if match: print(match) Output: data foo_matchme; set bar; run; data foo_matchme2; set bar; run;
Python: How much space does each element of a list take?
extract the number and name of python method arguments
Printing 5 numbers in a row
Django ValueError at /admin/
Running Tests From a Module
Django ease of building a RESTful interface
How do I run all Python unit tests in a directory?
Bypass django form validation on new form instance
Set language code inside a view in django
psycopg2 disconnects from server
Python def function: How do you specify the end of the function?
How do you implement a web-based direct deposit/eCheck payment system?
how to convert a timedelta object into a datetime object
Get decorated function object by string name
Selenium RC: how to capture/handle error?
Why is Python a favourite among people working in animation industry?