Regex match anything in between involving 2 lines

Hello I have the following texts where I need to capture the last number 899.48, 238.84, 5111.87

Wholesale Market Service Charge reflecting electricity consumed before and 321,242.175 kWh at $0.0039 per kWh 899.48

Wholesale Market Service Charge or close your Toronto Hydro services is online. 711,242.175 kWh at $0.0039 per kWh 238.84

Wholesale Market Service Charge Are you moving? The fastest way to set up tra 601,242.175 kWh at $0.0039 per kWh 5,111.87

basically anything could be in between “Wholesale Market Service Charge” and the number of kWh (the bold part), the number of kwh will always be at the beginning of the second line. I need to match this way because there are other instances of this format: 711,242.175 kWh at $0.0039 per kWh 238.84 and I need to capture only the Wholesale Market Service Charge.
the regex I have now isn’t working:
(?<=Wholesale Market Service Charge )(?:.*\r?\n[\d,.]+\skWh\sat\s$[\d,.]+\sper\skWh )([\d,.]+)

can someone please help?

thanks!

have a check on:

(?<=Wholesale Market Service Charge)(.|\n)*?(?=[\d,.]+)

Thanks so much @ppr, sorry I meant to capture the last number in the second line. would you please use the below for testing instead?
“321,242.175 kWh at $0.0039 per kWh 899.48”
“711,242.175 kWh at $0.0039 per kWh 238.84”
“601,242.175 kWh at $0.0039 per kWh 5,111.87”
these parts start in the second line…

Wholesale Market Service Charge reflecting electricity consumed before and 321,242.175 kWh at $0.0039 per kWh 899.48

Wholesale Market Service Charge or close your Toronto Hydro services is online. 711,242.175 kWh at $0.0039 per kWh 238.84

Wholesale Market Service Charge Are you moving? The fastest way to set up tra 601,242.175 kWh at $0.0039 per kWh 5,111.87

Please mark bold the needed one. Thanks

Wholesale Market Service Charge reflecting electricity consumed before and
321,242.175 kWh at $0.0039 per kWh 899.48

Wholesale Market Service Charge or close your Toronto Hydro services is online.
711,242.175 kWh at $0.0039 per kWh 238.84

Wholesale Market Service Charge Are you moving? The fastest way to set up tra
601,242.175 kWh at $0.0039 per kWh 5,111.87

thanks!!

Hello @lynnsong986

I might have a solution for you. Preview the pattern here. Please try this pattern:
(?<=Wholesale Market Service Charge.*)[\d,\.]+(?=\r|$)


grafik

Works like a charm as usual, thanks so much again!!

thanks @Steven_McKeering, really appreciate it, but somehow I couldn’t make it work on my end.

@lynnsong986
Keep the pattern from Steven still in mind. It has an approach that can handle some Windows Linebreaks specifics more defensively.

1 Like

Always happy to help @lynnsong986

I have amended the Regex Pattern as I missed a couple characters :sweat_smile: Preview link.
(?<=Wholesale Market Service Charge.*\n*)[\d,\.]+(?=[\r\n]|$)
Note: There was no line return in your sample when copied to Regex101.com etc. So I wasn’t sure if it was actually there or not. I copied your sample directly and built from that.

As per @ppr’s reply. Please keep it mind if you need a second pattern in the future :blush:

Have a nice day :slightly_smiling_face:

You people are awesome, I’ve learnt so much from you! tons of thanks!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.