简体   繁体   中英

Regular expression cant be extended to find similar pattern in Python

i am trying to match a pattern of ( dd/yy - dd/yy ) or (0d/yyyy - 0d/yyyy). Any possible combinations of the similar pattern in the text.

My text is:

 text  = ''' 09/14 - 18/18 some text 09/13 – 04/14 '''

My pattern is:

 r"b([\d]{1,2}[\s/-]+[\d]{2,4}[-:\s.]+\d{1,2}[\s/-]+[\d]{2,4}[\s/-]+)"

It successfully matches 09/14 - 18/18 . But not matching 09/13 – 04/14 in the text. Another doubt I am having is , if I want to check if say "09/14 - " is followed by a "word (any word in a list)" or a pattern like "dd/yy or dd/yyyy" what should I do. My point is if it matches any of the either check I have to pick that with "09/14". ie, "09/14 - word" if it matches or "09/14 - dd/yyyy" if it matches .

It successfully matches 09/14 - 18/18 . But not matching 09/13 – 04/14 in the text.

Because - and are different characters.

\b([\d]{1,2}[\s/-]+[\d]{2,4}[-:–\s.]+\d{1,2}[\s/-]+[\d]{2,4}[\s/-]+)

DEMO

Use this if you don't want to capture the spaces following the match.

\b([\d]{1,2}[\s/-]+[\d]{2,4}[-:–\s.]+\d{1,2}[\s/-]+[\d]{2,4})\b

DEMO

if I want to check if say "09/14 - " is followed by a "word (any word in a list)" or a pattern like "dd/yy or dd/yyyy" what should I do.

\b[\d]{1,2}[\s/-]+[\d]{2,4}[-:–\s.]+(?:\d{1,2}[\s/-]+[\d]{2,4}|\w)

DEMO

>>> text  = ''' 09/14 - 18/18 some text 09/13 – 04/14 '''
>>> re.findall(r'\b([\d]{1,2}[\s/-]+[\d]{2,4}[-:–\s.]+\d{1,2}[\s/-]+[\d]{2,4})\b', text)
['09/14 - 18/18', '09/13 – 04/14']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM