#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    26
    Rep Power
    0

    Question Regular Expressions Problem


    I cannot figure this out. I am trying to make a templating program, and I want to make a it process code blocks like this one...
    Code:
    {iterate name="i" list="list1"}
    <tr><td>{list1[i]}</td></tr>
    {/iterate}
    using this : re.compile("\{iterate name=\"(.*)\" list=\"(.*)\"\}(.*)\{/iterate\}", re.DOTALL)

    It works fine with just one block, but put two or more in a file and the regex finds from the first {iterate...} to the very last {/iterate}, ignoring any other closing or new iterate tags in between.

    How can I modify this regex to stop after it reaches the first {/iterate} tag it finds? I am using python 2.2 in cgi and note and am somewhat new to python and pretty new to regex.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    One answer is to make the match non-greedy:
    "\{iterate name=\"(.*?)\" list=\"(.*?)\"\}(.*?)\{/iterate\}"

    grimey
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    26
    Rep Power
    0
    Thank you very much, just what I was looking for. I'll have to read up on greediness/laziness.

IMN logo majestic logo threadwatch logo seochat tools logo