regexp-substr

REGEXP_SUBSTR assistance


This is probably pretty simple for most, but I'm just not great at regular expressions.

I have a set of strings with this type of format:

SYND_YAHOO_7BEST_RANKADDITIONS_268_2357425

So basically I want to search the string and extract the pattern _###_. Note that there can be 1 digit, or 2 or 3 or 4, etc. I know REGEXP_SUBSTR will do it for me, just not sure of the simple expression to use.

Thanks!


Solution

  • Assuming 268 is your desired match, a fairly simple pattern will work. Below returns the group of digits between underscores at the end of the string in the first capture group.

    .*_(\d+)_.*$

    https://regex101.com/r/gMCzdD/1

    The main key here is that we start with .* which matches aggressively "as many times as possible" meaning that we'll capture the last occurrence of digits between underscores, even if the same pattern occurs earlier in the string.