Retrieving the movie title and year from the string was valid

I miss the really obvious thing here, but I'm new to regex, so please; -)

I have several films in any format, which may or may not be attached to the year.

My Movie Name 2010 Some.Other.Super.Cool.Movie The~Third|Movie.2010 

Now, using (.+)\W(\d{4}) , I can extract two films with dates into two groups, one of which contains a name and the other a year, but the middle one is ignored? I am just a little sure how to make the segment a year optional.

Ideally ;-), I could use one expression to return names with \ W converted to spaces, but that's a different matter.

Thank you in advance

0
source share
2 answers

using? after a group of characters makes it optional, so in your case after (\ d {4})

 (.+)\W(\d{4})? 

This is because you are using greedy matching on (. +), And \ W includes a new line character in it (I think it is at least). Separate your line of trailing spaces, and if that doesn't work, make (. +) Lazy with? (. +?) - Also consider that \ W may be the wrong separator for this problem.

Also, adding $ to the end can help, since this will require numbers to complete the function, they can, try lazing matching and $.

 (.+?)\W(\d{4})?$ 
+2
source

? Makes this optional

 (.+?)\W?(\d{4})?$ 
0
source

Source: https://habr.com/ru/post/1240300/


All Articles