Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78825 > unrolled thread

help with regex

Started byJames Smith <bjlockie@lockie.ca>
First post2014-10-07 21:25 -0700
Last post2014-10-08 18:42 +1100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  help with regex James Smith <bjlockie@lockie.ca> - 2014-10-07 21:25 -0700
    Re: help with regex Peter Otten <__peter__@web.de> - 2014-10-08 08:38 +0200
    Re: help with regex Ben Finney <ben+python@benfinney.id.au> - 2014-10-08 18:42 +1100

#78825 — help with regex

FromJames Smith <bjlockie@lockie.ca>
Date2014-10-07 21:25 -0700
Subjecthelp with regex
Message-ID<d68610fc-b22b-4dbe-994d-177b28932347@googlegroups.com>
I want the last "1"
I can't this to work:

>>> pattern=re.compile( "(\d+)$" )
>>> match=pattern.match( "LINE: 235 : Primary Shelf Number (attempt 1): 1")
>>> print match.group()

[toc] | [next] | [standalone]


#78827

FromPeter Otten <__peter__@web.de>
Date2014-10-08 08:38 +0200
Message-ID<mailman.14672.1412750317.18130.python-list@python.org>
In reply to#78825
James Smith wrote:

> I want the last "1"
> I can't this to work:
> 
>>>> pattern=re.compile( "(\d+)$" )
>>>> match=pattern.match( "LINE: 235 : Primary Shelf Number (attempt 1): 1")
>>>> print match.group()

>>> pattern = re.compile("(\d+)$")
>>> match = pattern.search( "LINE: 235 : Primary Shelf Number (attempt 1): 
1")
>>> match.group()
'1'


See <https://docs.python.org/dev/library/re.html#search-vs-match>

[toc] | [prev] | [next] | [standalone]


#78828

FromBen Finney <ben+python@benfinney.id.au>
Date2014-10-08 18:42 +1100
Message-ID<mailman.14673.1412754179.18130.python-list@python.org>
In reply to#78825
Peter Otten <__peter__@web.de> writes:

> >>> pattern = re.compile("(\d+)$")
> >>> match = pattern.search( "LINE: 235 : Primary Shelf Number (attempt 1): 1")
> >>> match.group()
> '1'

An alternative way to accomplish the above using the ‘match’ method::

    >>> import re
    >>> pattern = re.compile("^.*:(? *)(\d+)$")
    >>> match = pattern.match("LINE: 235 : Primary Shelf Number (attempt 1): 1")
    >>> match.groups()
    ('1',)

> See <https://docs.python.org/dev/library/re.html#search-vs-match>

Right. Always refer to the API documentation for the API you're
attempting to use.

-- 
 \            “Without cultural sanction, most or all of our religious |
  `\          beliefs and rituals would fall into the domain of mental |
_o__)                                 disturbance.” —John F. Schumaker |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web