Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2004, 04:47 AM
deflatormouse deflatormouse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 26 deflatormouse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old November 19th, 2004, 05:34 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
One answer is to make the match non-greedy:
"\{iterate name=\"(.*?)\" list=\"(.*?)\"\}(.*?)\{/iterate\}"

grimey
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old November 20th, 2004, 12:49 AM
deflatormouse deflatormouse is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 26 deflatormouse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much, just what I was looking for. I'll have to read up on greediness/laziness.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Regular Expressions Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT