1

I have the following sentence:

Dave put the rubbish in the {{ if dave_good }}bin{{ else }}street{{ endif }}.

I'm currently replacing variables in text strings by capturing [[ something ]] and replacing the whole instance with a value (not a problem). But that's using python's re library.

I was wondering if someone could show me how to:

  1. search for single instances of {{ if dave_good }} in the string
  2. count forward from the {{ if dave_good }} to make sure there is a {{ endif }} before the end of the string
  3. if there is no {{ else }} then remove the tags, or the tags and their content, (at their location in the text) based on the boolean attribute of dave_good
  4. if there is an {{ else }}
    1. if dave_good is TRUE then remove {{ if dav_good }} and {{ else }}street{{ endif}}
    2. if dave_good is FALSE then remove {{ endif }} and {{ if dave_good }}bin{{ else }}
Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88

2 Answers2

1

try with Regular Expressions.

Example:

import re

result = re.match("\{\{((if|else|endif).*?)\}\}","{{if 100 > 1}}",re.I)
print result.groups()

result = re.match("\{\{((if|else|endif).*?)\}\}","{{else}}",re.I)
print result.groups()

result = re.match("\{\{((if|else|endif).*?)\}\}","{{endif}}",re.I)
print result.groups()

Or use a template engine for python

  1. jinja
  2. cheetah
  3. Templating
  4. bottlepy

Good Luck!

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
  • @LukasGraf I never said that was the way I just said it was an option, and that according to user needs. – Olaf Erlandsen Nov 04 '13 at 17:24
  • 1
    Still, it's a dead end that doesn't allow for solving the general case, and regular expressions are simply the wrong tool for this job. Regular expressions are for matching **regular** patterns. A template language, even a dead simple one, is at least a **context free** language. See [Chomsky Hierarchy](http://en.wikipedia.org/wiki/Chomsky_hierarchy) and [Can regular expressions be used to match nested patterns?](http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns) – Lukas Graf Nov 04 '13 at 17:28
  • +1 only for the template engine references – shad0w_wa1k3r Nov 04 '13 at 17:31
  • Yeah Lukas is right. Regular expressions are great for replacing all references to a variable in a string, but not good when you want to know where you are when your in a piece of code – Robert Johnstone Nov 05 '13 at 10:08
1

If you want to achieve only that on python then Here is something I wrote.
if its what you are asking.

def process(string,dave_good):
    ls=string.split(" ")
    flag=0
    flags=1
    for i in range(0,len(ls)-1):
        if ls[i]=='if' and ls[i+1]=='dave_good': #conditional if found.
            flag=1
            flags=1
            break
        flags=0

    if(flag): #expects a whitespace after endif (if theres a period then remove the     comments)
        st=i
        try:
            end=ls.index('endif')
        except:
            flags=0
            pass
        #    try:
         #       end=ls.index('endif.')
         #   except:
          #      print "no matching endif"
           #    flags=0

    if(flags):# delete 'if','else','endif' and corresponding variables(bin/streets)
        if dave_good:
            del ls[st]
            del ls[st]
            del ls[st+1]
            del ls[st+1]
            del ls[st+1]
        else:
            del ls[st]
            del ls[st]
            del ls[st]
            del ls[st]
            del ls[st+1]
        return " ".join(ls)#join back to a string

print process('Dave put the rubbish if in the if dave_good bin else street endif .',True)
print process('Dave put if else the rubbish in the if dave_good bin else street endif',False)


It might not be what exactly you are looking for but it can be a good start
Also for the third condition you can put a check for else condition and remove data similarly

xor
  • 2,668
  • 2
  • 28
  • 42