help with regex

Ben Finney ben+python at benfinney.id.au
Wed Oct 8 03:42:43 EDT 2014


Peter Otten <__peter__ at 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




More information about the Python-list mailing list