-1

I am trying to create a Regex that will match a file that has date in the middle: The filename looks something like this

"Weekly Score 07.27.2018 Report.csv"
"Weekly Score 08.03.2018 Report.csv"

The folder can have multiple files like these, I want to match any file with this pattern.

I am writing a python code for this on a server.

Aakib
  • 79
  • 1
  • 14

3 Answers3

0

Use pattern r"\s\d{2}\.\d{2}\.\d{4}\s"

Ex:

import re

l = ["Weekly Score 07.27.2018 Report.csv", "Weekly Score 08.03.2018 Report.csv", "Weekly Score Report.csv"]
for i in l:
    m = re.search(r"\s\d{2}\.\d{2}\.\d{4}\s", i) #or m = re.match(r"Weekly Score \d{2}\.\d{2}\.\d{4} Report.csv", i)
    if m:   #Has Date in name.
        print(m.group())

Output:

 07.27.2018 
 08.03.2018 
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Use (0?[1-9]|1[012]).(0?[1-9]|[12][0-9]|3[01]).\d{4}:

import re

files = ["Weekly Score 07.27.2018 Report.csv", "Weekly Score 08.03.2018 Report.csv"]
result = []

for filename in files:
    m = re.search(r"\s(0?[1-9]|1[012])\.(0?[1-9]|[12][0-9]|3[01])\.\d{4}\s", filename)
    if m:
        result.append(m.group())

print(result)
  • (0?[1-9]|1[012]) - months (from 01 to 12)
  • (0?[1-9]|[12][0-9]|3[01]) - days (from 01 to 31).
  • \d{4} - years.
Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24
  • What if I want to neglect the search from date point of view to just numeric values. Something like this [Weely] [Score] [Search field that can have 2 digits for month.field that can have 2 digits for day.field that have 4 digits for year] [Report].csv – Aakib Aug 14 '18 at 18:41
  • @Aakib than it should be simple regex with `\d{n}` for n-length number instead of patterns for days and months ranges. – Lev Zakharov Aug 14 '18 at 18:44
  • Solution: Weekly Score [0-9]{2}.[0-9]{2}.[0-9]{4} Report.csv – Aakib Aug 14 '18 at 20:13
  • @Aakib you can write `\d{n}` instead of `[0-9]{n}`. In OP you said, that you need to find a date. Anyway, good luck! – Lev Zakharov Aug 14 '18 at 20:35
0

You should not use Regex to match datetime as that could lead to false positives.

Get files using glob, iterate over them, check for the datetime pattern with datetime.datetime.strptime on the space separated third field:

import glob
import datetime

files = glob.iglob('Weekly Score * Report.csv')  # Make necessary changes
                                                 # on the pattern, if you want
for file_name in files:
    try:
        datetime.datetime.strptime(file_name.split()[2], '%m.%d.%Y')
    except ValueError:
        continue
    # Matched; do stuffs
    print(file_name)

Example:

In [960]: files = ["Weekly Score 07.27.2018 Report.csv", "Weekly Score 08.03.2018 Report.csv", "Weekly Score 15.23.2018 Report.csv"]

In [961]: for file_name in files:
     ...:     try:
     ...:         datetime.datetime.strptime(file_name.split()[2], '%m.%d.%Y')
     ...:     except ValueError:
     ...:         continue
     ...:     print(file_name)
     ...: 
     ...:         
Weekly Score 07.27.2018 Report.csv
Weekly Score 08.03.2018 Report.csv
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • What if I want to neglect the search from date point of view to just numeric values. Something like this [Weely] [Score] [Search field that can have 2 digits for month.field that can have 2 digits for day.field that have 4 digits for year] [Report].csv – Aakib Aug 14 '18 at 18:41
  • @Aakib It's pretty simple: match the pattern in strptime. I suggest you to look at the [`datetime.datetime.strptime` doc](https://docs.python.org/3.7/library/datetime.html#datetime.datetime.strptime). – heemayl Aug 14 '18 at 19:22
  • Solution: Weekly Score [0-9]{2}.[0-9]{2}.[0-9]{4} Report.csv – Aakib Aug 14 '18 at 20:14
  • 1
    @Aakib Well...You seemed to misunderstand the point of my answer and my _urge_ to not use Regex to match any datetime. Does the _solution_ you provided account for a file like `Weekly Score 99.99.9999 Report.csv` ? It's not a valid datetime but your solution would match it happily. Anyways, good luck! – heemayl Aug 14 '18 at 20:17