Home > Enterprise >  How to get everything before the second-to-last occurrence with a regex?
How to get everything before the second-to-last occurrence with a regex?

Time:09-17

I need everything that's behind the second-to-last comma and space of my string.

Example:

input: Jan 23, 2023, October 23, 2022, 2 October 23, 2022, Jan 23, 2023

output: Jan 23, 2023, October 23, 2022, 2 October 23, 2022

I tried this but this gives me the contrary result and it doesn't handle the space.

([^,] ),[^,] $

https://regex101.com/r/IPFj0y/1

CodePudding user response:

This seems to do the trick in the first capturing group:

(.*)(?=(, .*, ))

Explanation: first capture group captures everything up until the positive lookahead group (?=). Inside the positive lookahead I added the second to last rule you seeked, which is comma and space followed by anything and then another comma and space.

CodePudding user response:

You can match using this regex:

(.*\b\d{4}),.*

and replace with '$1.

RegEx Demo

RegEx Breakup:

  • (.*\b\d{4}): Match any text until last 4 digits. Due to greedy nature of .* quantifier it will match longest possible string before matching last 4 digits
  • ,: Match a comma
  • .*: Match everything till end
  • Related