'regex, catch the indentation before a tag

I'm trying to catch the indentation and the content followed by a banner.
for example, in the following ligne :\

"\n        hello world <tag>"\

I want to catch : the whitespace charaters between '\n' and 'hello' them the content (hello world).
Also, I don't want to catch these informations if the banner is not present.
ex, in:\

        poom \n     papoom\n    tocom <tag>\n

I want to match

'\n     tocom <tag>'

So I made the following regex query :

(\\n)([\t|\s]*?)(\w(?:.*?))(?=<tag>)

But I have an issue with the greedy match after the indentation. The query take the first '\n' of the sentence and not the closet to the banner. In my previous example, the match is :

\n     papoom\n    tocom <tag>\n

Is there any way to solve this issue ? (also I work with python regex)

ps : It is to replace a python line by another one.
ex :

\n      print("hello")<tag>

So, I really need to catch the indentation in the line I want to replace, otherwise, my code won't work. The difficulty comes from the dot symbol. And I can't catch the line, then the indentation with two regex queries, because I need to catch the line and the indentation to be able to use them with a banner \1 in re.sub.



Solution 1:[1]

Probably 80 different approaches to the problem.

How well does this approach work for you?

(?<BeforeTag>\\n(?:\s+\w+)+\s<tag>)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darin