6

New to python, competent in a few languages, but can't see a 'snazzy' way of doing the following. I'm sure it's screaming out for a regex, but any solution I can come up with (using regex groups and what not) becomes insane quite quickly.

So, I have a string with html-like tags that I want to replace with actual html tags.

For example:

Hello, my name is /bJane/b.

Should become:

Hello, my name is <b>Jane</b>.

It might be combo'd with [i]talic and [u]nderline as well:

/iHello/i, my /uname/u is /b/i/uJane/b/i/u.

Should become:

<i>Hello</i>, my <u>name</u> is <b><i><u>Jane</b></i></u>.

Obviously a straight str.replace won't work because every 2nd token needs to be preceeded with the forwardslash.

For clarity, if tokens are being combo'd, it's always first opened, first closed.

Many thanks!

PS: Before anybody gets excited, I know that this sort of thing should be done with CSS, blah, blah, blah, but I didn't write the software, I'm just reversing its output!

4
  • 6
    If that's supposed to be HTML then it's invalid because your elements are overlapping. I think what you meant was <b><i><u>Jane</u></i></b> but that might be more difficult to do correctly.
    – Mark Byers
    Mar 16, 2011 at 20:47
  • I am aware that it's not valid HTML, but that doesn't matter. I was just looking for a quick and dirty parser for the output from an app (new to python so thought I'd give it a shot). The result of the parse is only being used in house for notes.
    – Bridgey
    Mar 16, 2011 at 20:51
  • "...is only being used in house for notes" - can we hold you to that? :)
    – typo.pl
    Mar 16, 2011 at 20:56
  • @typo.pl lol, yeah, promise! Pinkie-swear :D
    – Bridgey
    Mar 16, 2011 at 21:16

3 Answers 3

7

Maybe something like this can help :

import re


def text2html(text):
    """ Convert a text in a certain format to html.

    Examples:
    >>> text2html('Hello, my name is /bJane/b')
    'Hello, my name is <b>Jane</b>'
    >>> text2html('/iHello/i, my /uname/u is /b/i/uJane/u/i/b')
    '<i>Hello</i>, my <u>name</u> is <b><i><u>Jane</u></i></b>'

    """

    elem = []

    def to_tag(match_obj):
        match = match_obj.group(0)
        if match in elem:
            elem.pop(elem.index(match))
            return "</{0}>".format(match[1])
        else:
            elem.append(match)
            return "<{0}>".format(match[1])

    return re.sub(r'/.', to_tag, text)

if __name__ == "__main__":
    import doctest
    doctest.testmod()
0
0

with sed:

s/\/([biu])([^/]\+)\/\1/<\1>\2<\/\1>/g
2
  • since he mentioned python, doesn't seem as helpful with a sed solution.
    – typo.pl
    Mar 16, 2011 at 20:55
  • Thanks guys. Pardon my python/sed ignorance, but I know sed as a linux/unix command, not part of python. Given that I'd like the python script to be able to run on Windows too is this going to work?
    – Bridgey
    Mar 16, 2011 at 21:07
0

A very simple solution would be to split the string using the source tag '/b' and rejoin the array of substring with the new destination tag '' like this:

s = "Hello, my name is /bJane/b."
'<b>'.join(s.split('/b'))
print s

'Hello, my name is <b>Jane<b>.'
1
  • You're missing the '/' in the closing tag.
    – johnsyweb
    Mar 17, 2011 at 20:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.