Rearding Regex (python)
Solution 1:
Here: http://rubular.com/r/CCW7sAUMrs is an example regex that matches whole text within {{ }}
.
You can easily clean up Your string with it using re.sub
.
Note, that it will not work when You have text like {{test}} paragraph {{test}}
, because it will match whole string.
As larsmans said, it gets harder if You can nest braces. Then it is the job for some kind of Pushdown automaton.
Edit:
Here is the usage:
>>> text = """
... {{ name = name
... prodcuer =producer
... writer = writer
... language = {{english}}
... country = USA
... }}
... Here is text I Need This paragraph.;
... """
>>> import re
>>> exp = "\{\{.+\}\}"
>>> re.sub(exp, "", text, flags=re.S | re.I).strip()
'Here is text I Need This paragraph.;'
Solution 2:
This looks like some sort of template file. Python has multiple templating engines available, and if you use one that will allow {{
and }}
to mark the replacement text, you could just use the template engine to replace that whole thing with the empty string.
You are probably already using some sort of templating engine since you have that text there. Can you just use that engine to get rid of the {{
}}
?
Post a Comment for "Rearding Regex (python)"