'Regex choose based on string format
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wiktor Stribiżew |
