My regex is not working properly

后端 未结 1 573
悲哀的现实
悲哀的现实 2021-01-23 04:12

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

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-23 05:05

    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.

    0 讨论(0)
提交回复
热议问题