| | |
Getting the start / end of string in regex through match objects
Thread Solved |
•
•
Join Date: Aug 2005
Posts: 7
Reputation:
Solved Threads: 0
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
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
Hmm, got some goofy funny faces in your code and reworked it a little. Is this still correct?
Python Syntax (Toggle Plain Text)
""" 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.
May 'the Google' be with you!
•
•
Join Date: Aug 2005
Posts: 7
Reputation:
Solved Threads: 0
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 :
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 :
Python Syntax (Toggle Plain Text)
startpattern = re.compile(r"(\$\{list: ([a-z]*[0-9 ]*)\})") # Get All the match objects of ${list: listname} in template startgrps = startpattern.finditer(self.tmplstr) # Store the end/start indices of all start list Placeholders: ${list: listname} endindexofStart = [] startindexofStart = [] for grp in startgrps: startindexofStart.append(grp.start()) endindexofStart.append(grp.end()) pass
Last edited by vegaseat; Jan 22nd, 2006 at 11:39 am.
![]() |
Similar Threads
- removing the null terminator at the end of string (C)
- Sorting string data (C++)
- 2D-Array, switches, and importing from a file (C++)
Other Threads in the Python Forum
- Previous Thread: Is this correct ?
- Next Thread: Limiting the size of List
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv array beginner book builtin calculator change command converter countpasswordentry csv curved dan08 def dictionary dynamic edit enter event file float format function google homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook plugin prime programming py2exe pygame pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax table terminal text textarea threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython






