Regex choose based on string format

153 Views Asked by At

I have following formats of data:

CumulativeReport_cumulativeReportBins_CumulativeBinNetworksViews_totalSuccessfulHeartbeats_1
CumulativeReport_cumulativeReportBins_CumulativeBinNetworksViews_totalSuccessfulHeartbeats__1

I am using following regex:

^(.*)_(.*?_.*?)(_\d$|__\d$)

My requirement every time is to get CumulativeBinNetworksViews_totalSuccessfulHeartbeats. For first case its working fine but for second case its printing "totalSuccessfulHeartbeats_1". How to solve this.

1

There are 1 best solutions below

4
On

You can use

^(.*)_([^_]+_[^_]+)__?\d$

See the regex demo. Details:

  • ^ - start of string
  • (.*) - Group 1: any zero or more chars other than line break chars as many as possible
  • _ - an underscore
  • ([^_]+_[^_]+) - Group 2: one or more chars other than _, _ and one or more chars other than _
  • __? - one or two underscores
  • \d - a digit
  • $ - end of string.