I'm trying to write a regex to catch any use of private members in python, with the exception of function names.
For example, the following should return true:
a = __something__
b.__something()
__bla = 5
a[__bla__]
... etc etc
But the following should return false:
def __unicode__(self):
....
(because it has the "def" before it)
I wrote this expression:
regexp = re.compile(r'(?!def\s)[^a-zA-Z^_\s]__[a-zA-Z]')
And it works for most cases, but for some reason it always return false if there's a space before the private, eg this will not return true:
regexp.search("something = __private")
What am I doing wrong here? the "(?!def\s)" should not match if have "def " before it, and I handle spaces before the two underscores, eg inside "[^a-zA-Z^_\s]". so why isn't it working?
EDIT:
While the accepted answer is correct for regex, I recommend looking at Padraic Cunningham's answer for a better solution using ast. Thanks,