how compile re greed mode?

thinkerou
import re

s = "{'key': 1234}{'test': {xyz}123}"

regex = re.compile("{.+?}")

result = regex.findall(s)

print result

result is ["{'key': 1234}", "{'test': {xyz}"],
but I hope result is ["{'key': 1234}", "{'test': {xyz}123"],
so, change re.compile("{.+?}") to re.compile("{.+}"),
now result is ["{'key': 1234}{'test': {xyz}123}"],
but I not hope.

Avinash Raj

I suggest you to change your regex like,

>>> s = "{'key': 1234}{'test': {xyz}123}"
>>> re.findall(r'\{(?:\{[^{}]*}|[^{}])*}', string)
["{'key': 1234}", "{'test': {xyz}123}"]

Problem with the greedy and non-greedy regexes.

>>> re.findall(r'\{.*}', s)
["{'key': 1234}{'test': {xyz}123}"]

Here .* is greedy and it matches all the characters as much as possible upto the last }. So the match is from first { to the last } symbol. So you got the above as output.

>>> re.findall(r'\{.*?}', s)
["{'key': 1234}", "{'test': {xyz}"]

Here .*? will do a non-greedy match. So from the { symbol, it matches all the characters upto the first closing brace }. So you got the above as output.

Solution:

\{(?:\{[^{}]*}|[^{}])*}
  • \{ matches the literal { symbol.

  • (?:..) Called non-capturing group.

  • \{[^{}]*}|[^{}] means match a set {....} or | any character but not of { or } ([^{}]),

  • (?:\{[^{}]*}|[^{}])* zero or more times. That is, alteration helps to switch here and forth once a character is not matched. If the first pattern fails to match the character, then the next pattern will come and try to match. If both are not matched then the control tranfers to the follwoing pattern that is, } because we defined the non-capturing group to repeat zero or more times.

  • } Matches a literal closing bracket.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to take an element after a re.compile?

From Dev

How to use re.compile with class in BeautifulSoup

From Dev

how to re.compile(multiple Regexpression)

From Dev

how to use re.compile and re.sub in python?

From Dev

How can I compile Assimp with Netbeans in debug mode?

From Dev

How can I compile Assimp with Netbeans in debug mode?

From Dev

How to add a variable into my re.compile expression

From Dev

How to combine two re.compile regex in python3?

From Dev

How to force Gradle to re-compile all subprojects sources?

From Dev

Python: How to use re.compile after click()

From Dev

How to combine two re.compile regex in python3?

From Dev

How to strip all tags except br in re.compile python?

From Dev

How to use re.compile within a for loop to extract substring indices

From Dev

Python: Where to compile an RE?

From Dev

Python: Where to compile an RE?

From Dev

Re compile stored procedure

From Dev

How to re-bind a binding from a major mode

From Dev

How does npm know you're in dev/developing mode?

From Dev

How to force $scope to re-compile its contents and re-link directives

From Dev

How to compile c++ application into x64 mode in Windows 64bit machine?

From Dev

How to compile libprotobuf in Release/Debug x64 mode using Visual Studio 2013

From Dev

How to compile c++ application into x64 mode in Windows 64bit machine?

From Dev

Python: re.compile and re.sub

From Dev

Multiple repeat in re.compile

From Dev

Adding re.compile to list

From Dev

Minecraft Launcher re-compile

From Dev

Handlebars re-compile templates

From Dev

Re-compile VIM with options

From Dev

Minecraft Launcher re-compile

Related Related

HotTag

Archive