0

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])/
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
biggboss2019
  • 220
  • 3
  • 8
  • 30
  • Why not just, `/202206/`? – phatfingers Jun 15 '22 at 17:55
  • The reason is the month number will change in future so unfortunately cannot use /202206/ – biggboss2019 Jun 15 '22 at 17:57
  • 3
    Do I understand correctly from your examples that you want to match the *current* month (as opposed to the previous month)? At time of writing we are in the 6th month (June). If I understand correctly, you don’t need no regex and can just do `testString.contains(YearMonth.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("uMM")))`. – Ole V.V. Jun 15 '22 at 18:01
  • 5
    Regex is nothing more than a character matcher. It has no concept of newest, oldest, current, bigger, nor smaller. You need to read each line individually, parse the date and use Java logic operators to deduce the "latest" month. – MonkeyZeus Jun 15 '22 at 18:02
  • You could still do a simple string match with variables. `targetYear="2022"; targetMonth="06"; if (candidate.matches(targetYear + targetMonth)) {...}`. – phatfingers Jun 15 '22 at 18:04
  • @OleV.V. that was what I was going for as well. I'd just make it a bit simpler and use `YearMonth.now()`; I don't think the explicit `ZoneId` is necessary. – Rob Spoor Jun 15 '22 at 18:05
  • 2
    @RobSpoor That’s a matter of taste alright. Which month is the current month is time zone dependent, and I like to remind myself and my reader of that fact even though it takes a few extra characters. – Ole V.V. Jun 15 '22 at 18:07

1 Answers1

2

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" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154