KNOWLEDGE BASE ARTICLE

Capture Last Occurrence of a Value in Text Block

If you need to use Smart Seek to retrieve the last value of a certain structure in a block of text, you may have found this difficult. However, you can do it with 2 excellent pieces of regex syntax. These are the look ahead and the \Z (end of text) line terminator option.

Assuming we want to capture the last currency value in a block of text (eg. $2,397.00). We use a look ahead to tell the regex engine that we expect to find anything after our capture value except the '$' symbol. This ensures that the last value is the one captured.

We start with the regex for the structure of the currency we're looking for...

(\$\s{0,1}[\d\.,]{1,})

Then we append a look ahead of the structure that will never match the regex above...

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

Note the \Z at the end of the look ahead - this ties the value to the end of a multiline text block. Without it we would be performing our search line by line and we would probably retrieve the first occurrence instead of the last.

Now we just piece it all together!

REGEX((\$\s{0,1}[\d\.,]{1,})(?=([^\$]{1,})\Z))

And there you have it. We will capture the last occurrence of a currency value. Too easy!

Perhaps a more simplified method of the above (where no $ character is in use) is to find a numeric (currency) value that is pinned (using a regex look ahead) to the end of the text and must contain anything except a currency value. You end up with this...

REGEX(([\d\.,]{1,})(?=([^\d]{0,})\Z))

Link to this article http://umango.com/KB?article=95