473,543 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:parentls t}
an element ${elem:parentls t}
${/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(lstp attern_st)

# End of List
lstpattern_end = r"(\$\{/list:([a-z]*[0-9 ]*)\})"
lstpat_e = re.compile(lstp attern_end, re.I)
matchgrp_st = lstpat_st.searc h(tmplstr)
strt = matchgrp_st.sta rt()
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 1716
"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(pat tern, 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
3443
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 specific to that platform are welcome too
20
8039
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 matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
29
4281
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can...
10
1655
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 example, getting the title (<title>Page Title</title>) of the page and passing that to a label on the application). What would be the best method for doing...
4
3065
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 string ? A simple example will help. Thanks. ra294@hotmail.com
16
5849
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 with the following elements arr(0) = "MyString" arr(1) = 40 arr(2) = "Hello world"
8
1676
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 catch *all* class definition headers, # but it's pretty darn close. classexpr = '^\(*class ++\) *( *) *\(\(=.*\)?\):' classprog =...
4
2625
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 match is never returned. The string that I give to the regex is one that contains the entire contents of a text file. I'm using the multi-line...
14
4961
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 text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before...
0
7411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7354
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5888
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1824
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
643
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.