Negating part of a regular expression

xiº

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:'

Regular expression visualization

Debuggex Demo

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?

Wiktor Stribiżew

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Negating an Expression

From Dev

Regular expression to match part of word

From Dev

Regular expression for getting part of string

From Dev

Include matched part in regular expression

From Dev

Regular Expression to replace part of a string

From Dev

Extract a part of regular expression java

From Dev

Regular Expression to select part in HTML

From Dev

Regular expression to retain part of the string

From Dev

Replace part of the String by Regular Expression

From Dev

Select part of line in regular expression

From Dev

Regular Expression Get part of the match

From Java

How to get Captured Part of the Regular Expression in Nim

From Dev

Find which part of a regular expression caused a match

From Dev

regular expression to extract part of email address

From Dev

Regular Expression to match part or all of a string

From Dev

Regular expression matching all text without part

From Dev

Regular expression where part of the pattern is optional.

From Dev

Captured group in optional part of a regular expression

From Dev

Exclude part of string using Regular expression

From Dev

Part of regular expression should not be matching . or , in the end

From Dev

Notepad++ regular expression - replace a part of regex

From Dev

Regular expression matching all text without part

From Dev

Extracting a specific part of a string with regular expression in Python

From Dev

Find which part of a regular expression caused a match

From Dev

sed to edit only part of a file with regular expression

From Dev

Best regular expression for matching the domain part of emails

From Dev

Python regular expression find words with part of text

From Dev

Regular expression with optional part and negative lookahead

From Dev

C# Regular Expression and Replace on part of the match

Related Related

  1. 1

    Negating an Expression

  2. 2

    Regular expression to match part of word

  3. 3

    Regular expression for getting part of string

  4. 4

    Include matched part in regular expression

  5. 5

    Regular Expression to replace part of a string

  6. 6

    Extract a part of regular expression java

  7. 7

    Regular Expression to select part in HTML

  8. 8

    Regular expression to retain part of the string

  9. 9

    Replace part of the String by Regular Expression

  10. 10

    Select part of line in regular expression

  11. 11

    Regular Expression Get part of the match

  12. 12

    How to get Captured Part of the Regular Expression in Nim

  13. 13

    Find which part of a regular expression caused a match

  14. 14

    regular expression to extract part of email address

  15. 15

    Regular Expression to match part or all of a string

  16. 16

    Regular expression matching all text without part

  17. 17

    Regular expression where part of the pattern is optional.

  18. 18

    Captured group in optional part of a regular expression

  19. 19

    Exclude part of string using Regular expression

  20. 20

    Part of regular expression should not be matching . or , in the end

  21. 21

    Notepad++ regular expression - replace a part of regex

  22. 22

    Regular expression matching all text without part

  23. 23

    Extracting a specific part of a string with regular expression in Python

  24. 24

    Find which part of a regular expression caused a match

  25. 25

    sed to edit only part of a file with regular expression

  26. 26

    Best regular expression for matching the domain part of emails

  27. 27

    Python regular expression find words with part of text

  28. 28

    Regular expression with optional part and negative lookahead

  29. 29

    C# Regular Expression and Replace on part of the match

HotTag

Archive