# Regex to extract Date from filename (4digit beginning with 19 or 20)

**URL:** <https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446>\
**Category:** Support\
**Created:** [December 19, 2022, 4:45pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446 "2022-12-19T16:45:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdmp3](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rdmp3](https://community.mp3tag.de/u/rdmp3)\
**Post date:** [December 19, 2022, 4:45pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/1 "2022-12-19T16:45:51Z")

</div>

I'm trying to create an action for the Date tag where the four digit date is extracted from the filename,

such that it will satisfy both these conditions:

- any number string of 4 digits  
( ignoring number strings that are either more or less than 4 digits )
- string must start with either 19 or 20

AND failing to meet these conditions, the Date tag won't be replaced.

---

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 19, 2022, 4:47pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/2 "2022-12-19T16:47:31Z")

</div>

You are looking for numbers between 1900 until 2099?  
There are never classical pieces from 18xx or earlier?

---

<div class="post-metadata">

**Author:** ![rdmp3](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rdmp3](https://community.mp3tag.de/u/rdmp3)\
**Post date:** [December 19, 2022, 4:50pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/3 "2022-12-19T16:50:06Z")

</div>

I suppose 17 - 20 would be fine as well... Id seldom come across anything 18 or before.

so Yes, 1900-2099 , but 1700-2099 is fine too.

---

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 19, 2022, 4:54pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/4 "2022-12-19T16:54:20Z")

</div>

What, if your filename has multiple valid dates, like  
`Filename with date from 1701 over 1971 to 2022.mp3`

Do you want the first or the last date in such a case?

---

<div class="post-metadata">

**Author:** ![rdmp3](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rdmp3](https://community.mp3tag.de/u/rdmp3)\
**Post date:** [December 19, 2022, 4:55pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/5 "2022-12-19T16:55:43Z")

</div>

only the first if more than one .

---

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 19, 2022, 5:18pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/6 "2022-12-19T17:18:03Z")

</div>

You can try this action:  
Type: Format Value  
Field: `DATE` (or whatever you want as tag to save the year into it)  
Format string:  
`$regexp(%_FILENAME%,.*?((17|18|19|20)\d{2}).+,$1)`

to fullfill your condition

> AND failing to meet these conditions, the Date tag won't be replaced.

you have to (mandatory!) FILTER your songs BEFORE you use the above action, with  
`%_FILENAME% MATCHES ((17|18|19|20)\d{2})`  
otherwise you get the full filename for those not matching (not including 1700 until 2099) into your DATE.

As always for regular expressions:  
Please test it on your backuped test data before you use it on your collection.

---

<div class="post-metadata">

**Author:** ![rdmp3](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@rdmp3](https://community.mp3tag.de/u/rdmp3)\
**Post date:** [December 19, 2022, 5:26pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/7 "2022-12-19T17:26:08Z")

</div>

Thank you very much for your time and expertise, I will test this as you suggested, and will return to edit this comment and click "solution" .

---

<div class="post-metadata">

**Author:** ![LyricsLover](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@LyricsLover](https://community.mp3tag.de/u/LyricsLover)\
**Post date:** [December 19, 2022, 5:28pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/8 "2022-12-19T17:28:00Z")

</div>

Hint: I had to update the expression to get the FIRST (not the LAST) year as wished.

---

<div class="post-metadata">

**Author:** ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)\
**Post date:** [December 19, 2022, 5:53pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/9 "2022-12-19T17:53:03Z")

</div>

> [@LyricsLover](#):
>
> you have to (mandatory!) FILTER your songs BEFORE you use the above action, with  
> `%_FILENAME% MATCHES ((17|18|19|20)\d{2})`

And once you have done that you could easily import the date data with  
Convert\>Filename-Tag  
Mask: %year% %dummy%

---

<div class="post-metadata">

**Author:** ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)\
**Post date:** [January 18, 2023, 5:53pm UTC](https://community.mp3tag.de/t/regex-to-extract-date-from-filename-4digit-beginning-with-19-or-20/59446/10 "2023-01-18T17:53:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
