Regular expression to extract the year from a string

后端 未结 5 834
盖世英雄少女心
盖世英雄少女心 2020-12-19 22:54

This should be simple but i cant seem to get it going. The purpose of it is to extract v3 tags from mp3 file names in Mp3tag.

I have these strings I want to extract

5条回答
  •  情深已故
    2020-12-19 23:08

    A suggestion for a more generic solution, not sure if that is what you need. Valid years will always have the form 19xx or 20xx, and the years will be separated with a word-break character (something other than a number or a letter):

    \b(19|20)\d{2}\b
    

    This doesn't really care where in the tag the year appears. A simpler version that doesn't assume anything more than 4 digits in the year would be this expression:

    \b\d{4}\b
    

    The key here is the \b escape sequence, which matches any non-word character (word charaters are letters, digits and underscores), including parenthesis, of course.

    Would also like to recommend this site: http://www.regular-expressions.info/

提交回复
热议问题