Hmm, got some goofy funny faces in your code and reworked it a little. Is this still correct?
"""
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
"""
tmplstr = """
${name}
${listparentlst}
an element ${elemparentlst}
${/listparentlst}
${list:childlst}
an element ${elem:childlst}
${/list:childlst}
"""
import re
# Compile List Patterns
# Start of List
lstpattern_st = r"(\$\{list([a-z]*[0-9 ]*)\})" # had unbalanced ()
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 'start =', strt
matchgrp_e = lstpat_e.search(tmplstr)
end = matchgrp_e.end()
print 'end =', end
print 'tmplstr[%d:%d] =' % (strt, end)
print tmplstr[strt:end] # removed self. vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417