Ask for help on using re
Peter Pearson
pkpearson at nowhere.invalid
Thu Aug 5 11:00:15 EDT 2021
On Thu, 5 Aug 2021 02:40:30 -0700 (PDT), Jach Feng <jfong at ms4.hinet.net> wrote:
I want to distinguish between numbers with/without a dot attached:
>>> text = 'ch 1. is\nch 23. is\nch 4 is\nch 56 is\n'
>>> re.compile(r'ch \d{1,}[.]').findall(text)
['ch 1.', 'ch 23.']
>>> re.compile(r'ch \d{1,}[^.]').findall(text)
['ch 23', 'ch 4 ', 'ch 56 ']
I can guess why the 'ch 23' appears in the second list. But how to get
rid of it?
>>> re.findall(r'ch \d+[^.0-9]', "ch 1. is ch 23. is ch 4 is ch 56 is ")
['ch 4 ', 'ch 56 ']
--
To email me, substitute nowhere->runbox, invalid->com.
More information about the Python-list
mailing list