Reputation: 21
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?
Upvotes: 1
Views: 95
Reputation: 18490
How about letting the dot consume and capture (no lookarounds).
.*,([^,]+),
Obviously this only makes if at lesat two commas occure in the string.
Upvotes: 1
Reputation: 15482
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.
Upvotes: 2