1

I have string:

...<w:t> Name</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Cambria" w:hAnsi="Cambria"/><w:b/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>{{</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:i/><w:iCs/></w:rPr><w:t>test</w:t></w:r><w:r><w:rPr><w:i/><w:iCs/></w:rPr><w:t>.name</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>}} <w:t>....

And i need script which will delete all tags(<...>) between {{ and }} But do not delete between pairs of characters, e.g:

The result of:
{{ <wr> test.name1 <wr> }} <wr><wr> {{ <wr> test.name2 <wr> }} 
will be:
{{ test.name1 }} <wr><wr> {{ test.name2 }} 
not:
{{ test.name1 }} {{ test.name2 }} 

Thank you in advance!

1

3 Answers 3

1

If you don't need single regular expression, you may combine substitutions:

    import re
    s='{{ <wr> test.name1 <wr> }} <wr><wr> {{ <wr> test.name2 <wr> }}'
    re.sub(r'({{[^{}]+}})', lambda x: re.sub(r'<[a-zA-Z0-9:-]+>', '', x.group(0)), s)
    '{{  test.name1  }} <wr><wr> {{  test.name2  }}'
Sign up to request clarification or add additional context in comments.

1 Comment

thx! But that solution do not work for string from question :(
0

You can do like this:

import re

TAG_RE = re.compile(r'\{\{(\s*<[^>]+>|\s*)(\s*.*?\s*)(<[^>]+>\s*|\s*)\}\}')

def remove_tags2(text):
    return TAG_RE.sub('{{ \g<2> }}', text)

remove_tags2("sdfgsd {{ <wr> blablalba sdf asf asga sfas asd </wr> }} <wr><wr> {{<wr>alsdfhaksdhfkajg<wr>}}")

Output:

'sdfgsd {{  blablalba sdf asf asga sfas asd  }} <wr><wr> {{ alsdfhaksdhfkajg }}'

1 Comment

thx! But that solution do not work for string from question :(
0

Based on Eugene answer:

import re

s='...<w:t> Name</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Cambria" w:hAnsi="Cambria"/><w:b/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr><w:t>:</w:t></w:r><w:r><$

print re.sub(r'({{[^{}]+}})', lambda x: re.sub(r'<[^>]+>', '', x.group(0)), s)

output:

...<w:t> Name</w:t></w:r><w:r><w:rPr><w:rFonts w:ascii="Cambria" w:hAnsi="Cambria"/><w:b/><w:sz w:val="28"/><w:szCs w:val="28"/></w:rPr><w:t>:</w:t></w:r><w:r><w:rPr></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:b/><w:bCs/></w:rPr><w:t>{{ test.name }} <w:t>....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.