Python Forum

Full Version: Python regex with negative set of characters multiline
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have following text stored as a string:

Output:
final class myClass1 { const APPLICATION1 = 1; const APPLICATION2 = 2; } final class myClass2 { const APPLICATION3 = 3; const APPLICATION4 = 4; } final class myClass3 { const APPLICATION5 = 5; const APPLICATION6 = 6; }
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??
Something like this.
>>> import re
>>> 
>>> r = re.search(r"myClass2\s{(.*?)}", data, re.MULTILINE | re.DOTALL)
>>> print(r.group(1).strip())
Output:
const APPLICATION3 = 3; const APPLICATION4 = 4;
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 ;-)