Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 23rd, 2004, 02:16 PM
duniyadnd duniyadnd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 30 duniyadnd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 4 sec
Reputation Power: 0
Regular expression (group)

I know in php, you can get multi arrayed results with preg_match and preg_match_all.

I was wondering if that was similar in the group() function in python by creating a list.

Example, I have in the text:

"mary had a little lamb, and a little rabbit."

and I want to search for "little" and it's subsequent word and I want a list of the matches.

So I'd have something like this:

Code:

content = 'mary had a little lamb, and a little rabbit.'
required = '(little\s*?\w*?)\w'

p = re.compile(required, re.IGNORECASE)

gr = p.search(content)


#This is the part I don't know what to do!!!
print gr.groups(1)
print gr.group(1)


I want to create an output that would say:

little lamb
little rabbit

and so on.

How would I go about getting that?

Thanks

Last edited by duniyadnd : April 23rd, 2004 at 02:18 PM.

Reply With Quote
  #2  
Old April 23rd, 2004, 03:12 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Originally Posted by duniyadnd
I want to create an output that would say:

little lamb
little rabbit

and so on.

How would I go about getting that?

Thanks

This code:
Code:
content = 'mary had a little lamb, and a little rabbit.'
required = 'little\s+\w+(?=\W)'

p = re.compile(required, re.IGNORECASE)
gr = p.finditer(content)

for match in gr:
    print match.group(0)

outputs:
Code:
little lamb
little rabbit

Reply With Quote
  #3  
Old April 23rd, 2004, 05:57 PM
duniyadnd duniyadnd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 30 duniyadnd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 4 sec
Reputation Power: 0
aaaaaah, that worked, thanks.. t

Reply With Quote
  #4  
Old April 24th, 2004, 02:31 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
If you want to get a list instead of an itorator then use the findall() method instead, if you just want to loop over the values finditer() is fine but it lacks the flexability of a list in some respects... IMO of course

Take care guys,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #5  
Old April 24th, 2004, 09:14 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
Originally Posted by netytan
If you want to get a list instead of an itorator then use the findall() method instead, if you just want to loop over the values finditer() is fine but it lacks the flexability of a list in some respects... IMO of course
Mark.

re.finditer is, of course, better not when you want to loop over the actual matches but when you want the actual match objects (with ability to get subgroups, find start and end of matches, etc.) instead of a plain list with text.

Reply With Quote
  #6  
Old April 26th, 2004, 12:07 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ah, but a list is much easier to manipulate than an itorator! Which is why i find it better since most of the time i'll find myself wanting to do something else with the values . But for looping i agree with perc of course

Mark.

Reply With Quote
  #7  
Old April 26th, 2004, 09:13 AM
duniyadnd duniyadnd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 30 duniyadnd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 4 sec
Reputation Power: 0
That discussion helped even more.. Thanks guys..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Regular expression (group)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway