Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trouble with regex

Reply
Thread Tools

Trouble with regex

 
 
Fernando Rodriguez
Guest
Posts: n/a
 
      11-14-2003
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
 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      11-14-2003
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.

 
Reply With Quote
 
 
 
 
Jim Shapiro
Guest
Posts: n/a
 
      11-14-2003

"Fernando Rodriguez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments