-4

I need to identify pattern like this in a text file:

123/12 (6/26/2020) 134/01 0/0 1/0 134/67 345/89

digit/digit group is repeated 7 times with the first one has date in it.

4

1 回答 1

2

You could use regex instead of re and try something along the lines of:

^(\d+\/\d+) \((?1)\/\d+\)(?: (?1)){5}$

See the online demo

I'm not sure about the context you are planning to use it in so you might be interested in word-boundaries too.


Update:

Based on your comment, it looks like you need to try implement something like:

import regex
lst = ['123/12 (6/26/2020) 134/01 0/0 1/0 134/67 345/89', 'blabla', '123/12 (11/2/2019) 112/11 3/2 1/5 112/987 23/1']
new_lst = [x for x in lst if regex.match(r'^(\d+\/\d+) \((?1)\/\d+\)(?: (?1)){5}$', x)]
print(new_lst)

Gets you:

['123/12 (6/26/2020) 134/01 0/0 1/0 134/67 345/89', '123/12 (11/2/2019) 112/11 3/2 1/5 112/987 23/1']
于 2020-06-26T16:39:39.513 回答