Neil 在 2021年8月5日 星期四下午6:36:58 [UTC+8] 的信中寫道: > Jach Feng <jf...@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? > > > > --Jach > Does > > >>> re.findall(r'ch\s+\d+(?![.\d])',text) > > do what you want? This matches "ch", then any nonzero number of > whitespaces, then any nonzero number of digits, provided this is not > followed by a dot or another digit.
Yes, the result is what I want. Thank you, Neil! The solution is more complex than I expect. Have to digest it:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list