python


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;

Related Links

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?

Categories

HOME
jsf
numpy
ssas-2012
google-cloud-bigtable
npm
vhdl
ubuntu-16.04
memory-leaks
hana
zend-framework2
heap-memory
laravel-4
ios10
yocto
urlencode
outlook-web-addins
gatsby
data-synchronization
pdo
sendkeys
riak
anychart
tflearn
iis-10
sumo
visual-c++-2017
branch
bitcoin-testnet
pitest
hibernate-cache
svnkit
npm-shrinkwrap
large-data
openpgp
remote-server
jquery-cycle2
sidr
text-files
axis-labels
fltk
polyfills
socketscan
intellij-idea-2016
xbim
stress-testing
worker
openweathermap
proof
sapui
seamless-immutable
mbaas
jcreator
emoticons
system.web.ui.webcontrols
alter
xib
rmongodb
qregexp
amf
elastix
magento-1.4
portfolio
spservices
android-cursoradapter
jta
geodjango
p-np
apigee-baas
pisa
tilestache
named-ranges
left-recursion
gmaps4rails
scala-2.11
livechat
funq
author
csplit
getimagedata
colt
magic-numbers
dvcs
late-static-binding
lov
baucis
loop-invariant
layered
va-list
windows-update
shortcuts
shiva3d
sharpmap
mailcore
objcmongodb
device-emulation
gdataxml
zend-framework-modules
qsqltablemodel
mpmovieplayer
gdt
hinstance
querystringparameter
cookieless
ets
rijndael
carbide
surefire
google-instant
opcodes
data-execution-prevention
divx

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App