#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep 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
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    >>> 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 and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep 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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    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 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    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 06:02 PM. Reason: demonstrate that that works
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    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.
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Thanks ! It helped me understand

IMN logo majestic logo threadwatch logo seochat tools logo