Home > Blockchain >  Regex Reverse Search on a String
Regex Reverse Search on a String

Time:07-05

I'm trying to extract the second last value in a string so I can return the value "Minute". I'm using Zapier which only allows regex.

Example string:

Week,Day,Hour,Another,Hour,Minute,Second

I've got a positive lookahead working below that returns the value "Second", however I cannot figure out how to apply an n-1 style operator to return the second-last string value "Minute".

My regex:

(?!^\,)[^,]*$

Try it here: https://regex101.com/r/mXVg6W/1

Any ideas?

CodePudding user response:

You can use the following regex:

[^,] (?=(?:,[^,] ){1}$)

It extracts the first non-comma values [^,] occurring after a comma, a sequence of non-comma values and the end of string (?:,[^,] ){1}$. If you want preceeding values, it's sufficient to increment the value inside of the braces (the amount of ,string to be passed over).

Check the demo here.

CodePudding user response:

How about letting the dot consume and capture (no lookarounds).

.*,([^,] ),

See this demo at regex101

Obviously this only makes if at lesat two commas occure in the string.

CodePudding user response:

Does this work? [\d \,]{9}(\d)[\,\d]

  • Related