pulpassets
pulpassets

Reputation: 21

Regex Reverse Search on a String

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

Answers (3)

bobble bubble
bobble bubble

Reputation: 18490

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.

Upvotes: 1

lemon
lemon

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

Ryan Penfold
Ryan Penfold

Reputation: 803

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

Upvotes: 0

Related Questions