I am trying to build regex to find classes into python code without explicitly inheritance from object
.
import re
test_string = '''
class Test(object):
pass
class Test:
pass
'''
regex = r'class .*(?!\(object\)).*'
re.compile(regex).findall(test_string)
Which gives me:
'class Test(object):', 'class Test:'
But I need only 'class Test:'
.
At the same time positive lookahead works fine:
>>> print regex = r'class .*(?=\(object\)).*'
['class Test(object):']
What is the problem here?
You need to use the (?!.*\(object\))
negative lookahead after "class ":
class (?!.*\(object\)).*
See the regex demo
The .*(?!\(object\)).*
subpattern matches any 0+ characters other than a newline that is not followed with (object)
. It effectively grabs all the line up to its end, and does not find any (object)
after it. The second .*
does not even match anything as all the characters already "belong" to the first .*
.
In (?!.*\(object\))
, the check occurs after consuming class
+space, and fails the match once there is an (object)
somewhere further on the current line.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments