Regular expression to match latest/last month

385 Views Asked by At

I am new to regex and I would like to only match the latest/last month from below strings, as we can see only 202206 should be matched and in future if there is 202207 then it should only match 202207. For matching, after researching I found I can use below regex. However, the issue is instead of matching only the latest dates it is matching both dates. How should I make my regex to match the latest/last month string only? Is there any if condition in regex ? I referenced this post as well. However, the regex is for Perl and not for Java. The reason I would like to get Java is because I am facing similar issue as mentioned by SO user post. My ETL tool is different.

Thanks in advance!

test1-202201
test1-202206 

REGEX

/\d{4}(0[1-9]|1[0-2])/
1

There are 1 best solutions below

0
On BEST ANSWER

tl;dr

Use YearMonth class rather than regex.

YearMonth
.now( 
    ZoneId.of( "Africa/Casablanca" )
)
.isAfter(
    YearMonth
    .parse( "2022-06" )
)

Details

As the Comments mentioned, regex is a string comparison tool. Regex knows nothing about current and previous month. Regex is very powerful and interesting. But don’t fall into the trap of “If Your Only Tool Is a Hammer Then Every Problem Looks Like a Nail”.

For date-time work, use date-time classes. Fortunately, Java now offers the industry-leading date-time classes in the java.Time package, defined in JSR 310.

Never use use the legacy date-time classes found outside the java.time package. They are terrible, a masterclass in how to not do object-oriented programming.

For a year-month, use YearMonth class.

Determining the current year-month requires a time zone. For any given moment, the date varies around the globe by zone. So around the ending/beginning of the month, it may be “next month” in Tokyo Japan while simultaneously “last month” in Toledo Ohio US.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
YearMonth ym = YearMonth.now( z ) ;

For text, use standard ISO 8601 format: YYYY-MM.

String output = ym.toString() ; 

And parsing.

YearMonth ym = YearMonth.parse( "2022-06" ) ;