Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 November 20th, 2012, 01:55 PM
prodigyaj prodigyaj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 prodigyaj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 49 sec
Reputation Power: 0
Simple python regex doubt

Hi,

I am trying to get a match for the string "test?" Below compilation doesnt yield a match

regex = re.compile(r'\w\?')
print regex.match('test?')

Any leads on why? Sorry if this a very basic question as this my first attempt on python regex.

Thanks
- AJ

Reply With Quote
  #2  
Old November 20th, 2012, 02:11 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,386 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 5 m 35 sec
Reputation Power: 383
>>> import re
>>> re.search('test[?]',"this is a test?")
<_sre.SRE_Match object at 0x15e81d0>
>>> re.search('test[?]',"this is a test")
>>>
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 20th, 2012, 02:14 PM
prodigyaj prodigyaj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 prodigyaj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 49 sec
Reputation Power: 0
Thanks for the reply. Can you please explain in a couple of sentences why my code is not right and how is this piece of code u gave works?

Thanks again !
- AJ

Reply With Quote
  #4  
Old November 20th, 2012, 02:50 PM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
Quote:
Originally Posted by prodigyaj
Can you please explain in a couple of sentences why my code is not right and how is this piece of code u gave works?


Let the docs speak: “Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.”

http://docs.python.org/3/library/re.html
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #5  
Old November 20th, 2012, 04:48 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,386 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 5 m 35 sec
Reputation Power: 383
Your
r'\w'
matches only a single character, making SuperOscar's explanation dead on correct.

>>> re.match(r'\w+\?','thisisatest?')
<_sre.SRE_Match object at 0x1a1ff38>

The + permits 1 or more of prior expression.

Code:
>>> o = re.search(r'\w+\?','this is a test?')
>>> o.string[slice(*o.span())]
'test?'
>>> 
Comments on this post
prodigyaj agrees!

Last edited by b49P23TIvg : November 20th, 2012 at 05:02 PM. Reason: demonstrate that that works

Reply With Quote
  #6  
Old November 21st, 2012, 05:17 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 17 m 55 sec
Reputation Power: 65
I have to admit I didn’t even read very thoroughly the regex in question. When first I stumbled into this strange feature of Python’s .match() method – one I still quite can’t understand – I decided always to use .search() since it makes much more sense.
Comments on this post
b49P23TIvg agrees: re.match doesn't belong.

Reply With Quote
  #7  
Old November 21st, 2012, 11:09 AM
prodigyaj prodigyaj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 prodigyaj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 49 sec
Reputation Power: 0
Thanks ! It helped me understand

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Simple python regex doubt

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap