![]() |
Python regex with negative set of characters multiline - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python regex with negative set of characters multiline (/thread-11890.html) |
Python regex with negative set of characters multiline - sonicblind - Jul-30-2018 Hi, I have following text stored as a string: I am trying to search the middle block (myClass2), but this pattern does not work:block = re.search(r'final\sclass\smyClass2\s{([ˆ}]+)}', text, re.DOTALL)while the next one matches everything up until the very last curly bracket: block = re.search(r'final\sclass\smyClass2\s{(.+)}', text, re.DOTALL)Apparently the problem is with the [ˆ}]+ section of the pattern. Why does it not work as expected - to match any character except of ending curly bracket?? RE: Python regex with negative set of characters multiline - snippsat - Jul-30-2018 Something like this. >>> import re >>> >>> r = re.search(r"myClass2\s{(.*?)}", data, re.MULTILINE | re.DOTALL) >>> print(r.group(1).strip())
RE: Python regex with negative set of characters multiline - sonicblind - Jul-30-2018 Ah, damn! the greedy and non-greedy quantifiers, I totally forgot about them. Thanks a lot for the tip and reminder, it works now as expected ;-) |