Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78825 > unrolled thread
| Started by | James Smith <bjlockie@lockie.ca> |
|---|---|
| First post | 2014-10-07 21:25 -0700 |
| Last post | 2014-10-08 18:42 +1100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | James Smith <bjlockie@lockie.ca> |
|---|---|
| Date | 2014-10-07 21:25 -0700 |
| Subject | help 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]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-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]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-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