no_freedom
no_freedom

Reputation: 1961

My regex is not working properly

My regex is not working properly. I'm showing you before regex text and after regex text. I'm using this regex re.search(r'(?ms).*?{{(Infobox film.*?)}}', text). You will see my regex not displaying the result after | country = Assam, {{IND . My regex stuck at this point. Will you please help me ? thanks

Before regex:

    
{{Infobox film
| name           = Papori
| released       = 1986
| runtime        = 144 minutes
| country        = Assam, {{IND}}
| language       = [[Assamese language|Assamese]]
| budget         = 
| followed by    = free
}}

After regex:

    
{Infobox film
| name           = Papori
| released       = 1986
| runtime        = 144 minutes
| country        = Assam, {{IND

Why regex stuck at this point? country = Assam, {{IND

Edit : Expecting Result

Infobox film
    | name           = Papori
    | released       = 1986
    | runtime        = 144 minutes
    | country        = Assam, {{IND}}
    | language       = [[Assamese language|Assamese]]
    | budget         = 
    | followed by    = free

Upvotes: 2

Views: 272

Answers (1)

Graeme Perrow
Graeme Perrow

Reputation: 57248

Your regex is catching everything between the first {{ and the first }}, which is in the "country" entry of the infobox. If you want everything between the first {{ and the last }}, then you want to make the .* inside the braces greedy by removing the ?:

re.search(r'(?ms).*?{{(Infobox film.*)}}', text)

Note that this will find the last }} in the input (eg. if there's another template far below the end of the infobox, it will find the end of that), so this may not be what you want. When you have nesting things like this, regex is not always the best way to search.

Upvotes: 2

Related Questions