Getting the start / end of string in regex through match objects

Thread Solved

Join Date: Aug 2005
Posts: 7
Reputation: ankit_rastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankit_rastogi82 ankit_rastogi82 is offline Offline
Newbie Poster

Getting the start / end of string in regex through match objects

 
0
  #1
Jan 9th, 2006
Hi everybody,
I want to get the start and end of all the patterns mattched in regex. I know I can get it with start() and end() fn of matched objects. But re.search() return the match object of first matching regex in the string. I want all match objects in that string

Here is the string :

tmplstr = """
${name}

${list: parentlst}
an element ${elem: parentlst}
${/list: parentlst}

${list: childlst}
an element ${elem: childlst}
${/list: childlst}
"""

Here is the regex script:

# Compile List Patterns
# Start of List
lstpattern_st = r"(\$\{list: ([a-z]*[0-9 ]*)\})"
lstpat_st = re.compile(lstpattern_st)

# End of List
lstpattern_end = r"(\$\{/list: ([a-z]*[0-9 ]*)\})"
lstpat_e = re.compile(lstpattern_end, re.I)


matchgrp_st = lstpat_st.search(tmplstr)
strt = matchgrp_st.start()
print strt
matchgrp_e = lstpat_e.search(tmplstr)
end = matchgrp_e.end()
print end
print self.tmplstr[strt:end]


Note: There are no spaces in $list after colon. I had given it to avoid smilies only

I want all the start and end indices of the string but re.search() returns the first regex met in the string. re.match() also wont work because it search in the begining.

Can anyone help me in getting the start and end indices of all. OR can provide any other solution instead of this
Last edited by ankit_rastogi82; Jan 9th, 2006 at 11:02 am. Reason: edit
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Getting the start / end of string in regex through match objects

 
0
  #2
Jan 9th, 2006
Hmm, got some goofy funny faces in your code and reworked it a little. Is this still correct?
  1. """
  2. I want all the start and end indices of the string but re.search() returns the first
  3. regex met in the string. re.match() also wont work because it search in the begining.
  4.  
  5. Can anyone help me in getting the start and end indices of all. OR can provide any
  6. other solution instead of this
  7. """
  8.  
  9. tmplstr = """
  10. ${name}
  11.  
  12. ${listparentlst}
  13. an element ${elemparentlst}
  14. ${/listparentlst}
  15.  
  16. ${list:childlst}
  17. an element ${elem:childlst}
  18. ${/list:childlst}
  19. """
  20.  
  21. import re
  22.  
  23. # Compile List Patterns
  24. # Start of List
  25. lstpattern_st = r"(\$\{list([a-z]*[0-9 ]*)\})" # had unbalanced ()
  26. lstpat_st = re.compile(lstpattern_st)
  27.  
  28. # End of List
  29. lstpattern_end = r"(\$\{/list([a-z]*[0-9 ]*)\})"
  30. lstpat_e = re.compile(lstpattern_end, re.I)
  31.  
  32.  
  33. matchgrp_st = lstpat_st.search(tmplstr)
  34. strt = matchgrp_st.start()
  35. print 'start =', strt
  36. matchgrp_e = lstpat_e.search(tmplstr)
  37. end = matchgrp_e.end()
  38. print 'end =', end
  39. print 'tmplstr[%d:%d] =' % (strt, end)
  40. print tmplstr[strt:end] # removed self.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 7
Reputation: ankit_rastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankit_rastogi82 ankit_rastogi82 is offline Offline
Newbie Poster

Re: Getting the start / end of string in regex through match objects

 
0
  #3
Jan 11th, 2006
Hi vega,
for getting all the match objects in the string. I had found out finditer() and it gave me the result. Thanks for replying. Here is the script snippet :

  1. startpattern = re.compile(r"(\$\{list: ([a-z]*[0-9 ]*)\})")
  2.  
  3. # Get All the match objects of ${list: listname} in template
  4. startgrps = startpattern.finditer(self.tmplstr)
  5.  
  6. # Store the end/start indices of all start list Placeholders: ${list: listname}
  7. endindexofStart = []
  8. startindexofStart = []
  9. for grp in startgrps:
  10. startindexofStart.append(grp.start())
  11. endindexofStart.append(grp.end())
  12. pass
Last edited by vegaseat; Jan 22nd, 2006 at 11:39 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC