# REGEX to find YEAR in given string

**URL:** <https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522>\
**Category:** Support\
**Created:** [December 14, 2021, 3:53pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522 "2021-12-14T15:53:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roland](https://community.mp3tag.de/user_avatar/community.mp3tag.de/roland/32/8001_2.png) [@Roland](https://community.mp3tag.de/u/Roland)\
**Post date:** [December 14, 2021, 3:53pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/1 "2021-12-14T15:53:23Z")

</div>

I want to format the year (YYYY) from a string.

`$regexp(abc123197588trs,'^.*((19|20)(\d\d)).*$',$1)` returns **1975** as expected.

`$regexp(ab0244,'^.*((19|20)(\d\d)).*$',$1) ` returns **ab0244**.

What's wrong?

---

<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 14, 2021, 3:55pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/2 "2021-12-14T15:55:58Z")

</div>

> [@Roland](#):
>
> ab0244

Does not match as you have "02" instead of "20".

---

<div class="post-metadata">

**Author:** ![Roland](https://community.mp3tag.de/user_avatar/community.mp3tag.de/roland/32/8001_2.png) [@Roland](https://community.mp3tag.de/u/Roland)\
**Post date:** [December 14, 2021, 3:57pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/3 "2021-12-14T15:57:36Z")

</div>

If there is no match I expect to get no string back. 🤔

---

<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 14, 2021, 3:58pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/4 "2021-12-14T15:58:00Z")

</div>

no, if there is no match then the original string is returned

---

<div class="post-metadata">

**Author:** ![Roland](https://community.mp3tag.de/user_avatar/community.mp3tag.de/roland/32/8001_2.png) [@Roland](https://community.mp3tag.de/u/Roland)\
**Post date:** [December 14, 2021, 4:01pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/5 "2021-12-14T16:01:04Z")

</div>

Oops!

How to find a valid year (YYYY) in a string with no specific structure/content?

---

<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 14, 2021, 4:05pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/6 "2021-12-14T16:05:12Z")

</div>

I don't understand.  
Finding a valid year would then depend on the regular expression to return nothing if the pattern does not produce a hit?  
I find it actually as some kind of safety measure: if you have a non-matching expression, you don't loose data. So you could apply another expression to yield better results.

> [@Roland](#):
>
> How to find a valid year (YYYY) in a string with no specific structure/content?

If you have no structure in a string ... then I don't think that anyone would find anything like a "year" (and a spcificly a valid one) in that string.  
Could you explain a little more, please?

---

<div class="post-metadata">

**Author:** ![Roland](https://community.mp3tag.de/user_avatar/community.mp3tag.de/roland/32/8001_2.png) [@Roland](https://community.mp3tag.de/u/Roland)\
**Post date:** [December 14, 2021, 4:13pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/7 "2021-12-14T16:13:49Z")

</div>

I try to fill the tag YEAR.  
When performing an automated action and the field YEAR is empty I would like to scan \_dirname or \_filename for a valid value.  
Doing so I don't know if $REGEX will find something useful and leave the YEAR untouched when no match is given.

---

<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 14, 2021, 4:17pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/8 "2021-12-14T16:17:36Z")

</div>

I am always a friend of filters. With those you can leave out all the files that may lead to surprises - and I think that filling the year like that will happen once in the lifetime of a file. So it does not have to be the omnipotent action that takes into account all possibilities.

A filter like  
`%_path% MATCHES \d\d\d\d AND %year% MISSING`  
Should display all the candidates.

---

<div class="post-metadata">

**Author:** ![Roland](https://community.mp3tag.de/user_avatar/community.mp3tag.de/roland/32/8001_2.png) [@Roland](https://community.mp3tag.de/u/Roland)\
**Post date:** [December 14, 2021, 4:37pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/9 "2021-12-14T16:37:14Z")

</div>

**Thank you ohrenkino for your suggestíon.**  
It solves this issue in a specific situation.  
But in my case it is only one of 20 (or more) automated steps.

So the solution that I need is, to know if there is a number within a range from 1900 to 2099 in a string.  
If so I can use $REGEX alone.  
Otherwise I have to check the returned value again if it fits in the given boundaries.  
($num(string,4)) + $ifgreater(a,b,x,y) + ..  
If the found number is not in the given range YEAR must be reseted.  
Seems too complicated, is there no easy way to do this?

---

<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 14, 2021, 4:41pm UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/10 "2021-12-14T16:41:55Z")

</div>

you could test for the returned string with $num() - if that is 0 (which means there are leading non-numeric characters) then you could do more processing.

Still, I wonder why it has to be so complicated? Why devise 20 steps (or more) in 1 go - with a lot of thinking and trial and error going into it - instead of simple, dedicated actions that perform just one task. And I can define the einvironment and see which is the action to take.  
But here, everybody is the smith of his own workflow...

---

<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:** [March 1, 2025, 7:48am UTC](https://community.mp3tag.de/t/regex-to-find-year-in-given-string/55522/11 "2025-03-01T07:48:30Z")

</div>

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