text processing problem



Hi,

I'm looking for a way to do this: I need to scan a text (paragraph or so) and look for occurrences of "<text-x> (<text-x>)". That is, if the text just before the open bracket is the same as the text in the brackets, then I have to delete the brackets, with the text in it.

Does anyone knows any way to achieve this?

The closest I've seen is (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305306) by Raymond Hettinger

>>> s = 'People of [planet], take us to your leader.'
>>> d = dict(planet='Earth')
>>> print convert_template(s) % d
People of Earth, take us to your leader.

>>> s = 'People of <planet>, take us to your leader.'
>>> print convert_template(s, '<', '>') % d
People of Earth, take us to your leader.

"""

import re

def convert_template(template, opener='[', closer=']'):
    opener = re.escape(opener)
    closer = re.escape(closer)
    pattern = re.compile(opener + '([_A-Za-z][_A-Za-z0-9]*)' + closer)
    return re.sub(pattern, r'%(\1)s', template.replace('%','%%'))

Cheers
Maurice
.



Relevant Pages

  • Re: Effective use of the ^& find/replace wildcard
    ... Put (brackets) around parts you want to re-use. ... The paragraph mark ^13 gets deleted. ... > and replace them with a space character. ... > space since it appears in the regular expression. ...
    (microsoft.public.word.docmanagement)
  • Re: Primeval finale
    ... The paragraph ended with a full stop before the ... brackets began clearly indicated that they should not be considered ... In fact 3 episodes will not do it justice. ... you dance your silly little troll dance in reply on everything I type. ...
    (rec.arts.drwho)
  • Re: rand()
    ... It's an intrinsic 'feature' so if you've got a proper installation it should ... It has to be the first and only text in a new paragraph. ... > numbers inside the brackets. ... > by reinstalling Word 2000, ...
    (microsoft.public.word.docmanagement)
  • Re: How do I add brackets to multiple words in a WORD document?
    ... separate paragraph. ... you don't mind Jezebel, please continue assisting me with this issue - I ... indeed insert brackets around each paragraph, ... if you could explain what the characters ...
    (microsoft.public.word.docmanagement)
  • Re: Identify External Reference - Take 2
    ... Gord Dibben Excel MVP ... >Do a Find for an open bracket. ... >for the brackets you'll find the formula. ...
    (microsoft.public.excel.misc)