Try to search your question here, if you can't find : Ask Any Question Now ?

modification of making regex more specific to exclude certain characters

HomeCategory: stackoverflowmodification of making regex more specific to exclude certain characters
Avatarkundan asked 7 days ago
import re
s = '01.20/15 01/20.1915 1/1/19 01.11.11 1.8-24 3-4.12 01/20-09 12/12/1981 12-01/2001 1*51*12  22|1|13 03-02-1919 1-22-12 or 01-23-18 or 03-23-1984 01.11.18 or 2.2.17 or 02.02.18 or 12.1.16 12.23.1943 01-23-11 not 12.23.192 not 02.02.1'

I have the following string s which is a modification from making regex more specific to exclude certain characters

I want to get all dates so I use

reg = r'b((?:d{1,2}(?:.|/|-)){2}(?:d{4}|d{2}))b'
r1 = re.findall(reg,s)
r1

and I get

['01.20/15',
 '01/20.1915',
 '1/1/19',
 '01.11.11',
 '1.8-24',
 '3-4.12',
 '01/20-09',
 '12/12/1981',
 '12-01/2001',
 '03-02-1919',
 '1-22-12',
 '01-23-18',
 '03-23-1984',
 '01.11.18',
 '2.2.17',
 '02.02.18',
 '12.1.16',
 '12.23.1943',
 '01-23-11']

This is close to what I want but I do not want strings that have a mix of . or / or - e.g. '01.20/15' or '3-4.12' or 3-4.12 etc

My desired output is this

['1/1/19',
 '01.11.11',
 '12/12/1981',
 '03-02-1919',
 '1-22-12',
 '01-23-18',
 '03-23-1984',
 '01.11.18',
 '2.2.17',
 '02.02.18',
 '12.1.16',
 '12.23.1943',
 '01-23-11']

which does not contain any numbers that have a mix of . or / or - e.g. '01.20/15' or '3-4.12'

I have also tried modifying reg

reg = r'b((?:d{1,2}(?:.b|\b/|-b)){2}(?:d{4}|d{2}))b'

but this doesnt quite work

How do I further modify reg to get my desired output?

1 Answers
Best Answer
AvatarArben answered 7 days ago
Your Answer

18 + 9 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com