473,480 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with regex

Hi,

I'm trying to write a regex that finds whatever is between ${ and } in a text
file.

I tried the following, but it only finds the first occurrence of the pattern:
s = """asssdf${123} dgww${one} ${two}""" what = re.compile("\$\{([^}]*)\}")
m = what.search(s)
m.groups()

('123',)

What am I doing wrong? O:-)
Jul 18 '05 #1
2 1318
Fernando Rodriguez wrote:
Hi,

I'm trying to write a regex that finds whatever is between ${ and } in a text
file.

I tried the following, but it only finds the first occurrence of the pattern:

s = """asssdf${123}
dgww${one} ${two}"""
what = re.compile("\$\{([^}]*)\}")
m = what.search(s)
m.groups()


('123',)

What am I doing wrong? O:-)


Nothing ;) search just finds first match. If you want to find all
non-overlapping matches try finditer:

import re

s = """asssdf${123}

dgww${one} ${two}"""

what = re.compile("\$\{([^}]*)\}")
for m in what.finditer(s):
print m.groups()

prints:
('123',)
('one',)
('two',)

regards,
anton.

Jul 18 '05 #2

"Fernando Rodriguez" <fr*@easyjob.net> wrote in message
news:52********************************@4ax.com...
Hi,

I'm trying to write a regex that finds whatever is between ${ and } in a text file.

I tried the following, but it only finds the first occurrence of the pattern:
s = """asssdf${123} dgww${one} ${two}""" what = re.compile("\$\{([^}]*)\}")
m = what.search(s)
m.groups()
('123',)

What am I doing wrong? O:-)


Try:

import re

s = """asssdf${123}
dgww${one} ${two}"""
what = re.compile("\$\{([^}]*)\}") # same as original
m = what.findall(s)
print m
regex_test.py

['123', 'one', 'two']

HTH

Jim Shapiro
Jul 18 '05 #3

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

Similar topics

22
2738
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
6
1712
by: Just Me | last post by:
Any ideas on this. I am trying to loop through an xml document to remove attributes, but Im having so much trouble, any help is appreciated //THIS IS THE EXCEPTION ( SEE CODE LINE WHERE FAILURE...
2
1020
by: Roshawn | last post by:
Hi, I access a particular page by specifying the page's name and extension like so: http://localhost:xxxx/MyFiles/Lucky/ The Lucky directory contains a webpage named Default.aspx. I can...
0
2171
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
15
50116
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
12
3295
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
4
1350
Xx r3negade
by: Xx r3negade | last post by:
I'm using regular expressions to parse HTML hyperlinks and I've run into a problem. I'm trying to escape characters such as '.' and '?' for use in regular expressions, but it's not working #...
3
2185
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
5
13292
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
7032
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,...
0
7076
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...
1
6730
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...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
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,...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
174
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...

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.