473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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]

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

Jan 9 '06 #1
2 1701
"ankit" wrote:
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


for m in re.finditer(pattern, string):
print m.start(), m.end()

(or use the start offset to re.search)

</F>

Jan 9 '06 #2
Thanks frederik, :)

Jan 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Martijn | last post by:
Hi, Which is the prevalent way of matching a filename to a mask in runtime? The best I can think of, is sscanf. Thanks for the help! <OT> It's for the Windows platform, so any functions...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
10
by: Kris Rockwell | last post by:
Hello, I have put together a browser using the AXWebBrowser control. I would like to access elements of the displayed web page and pass that information back to the application (say, for...
4
by: Ya Ya | last post by:
Hi, I have a string with some fixed text and variable text. For example: "this is a fixed text THE NEEDED INFO more more fixed text". How do I get the the variable text (THE NEEDED INFO) from this...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
8
by: jlowery | last post by:
I'm looking through the tools/scripts folder from the python install, trying to get reacquanted with the language. Got a question on the following classfix.py snippet: # This expression doesn't...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.