Search code examples
regextableau-apiregex-lookaroundsdata-cleaning

return nth match from string using regex


I am using Tableau to create a visualization and need to apply Regex to string values in my data set. I'm trying to use Regex to return the nth match of this string of data: b29f3b2f2b2f3b3f1r2f3+b3x#. The data will always be in one line and I need to break the data out into substrings each time the characters b,s,f, or d are encountered and I need to match the nth occurrence returned. For example, when identifying which number match to return the following will match:

  • n=1 matches b29
  • n=2 matches f3
  • n=3 matches b2
  • n=4 matches f2
  • n=5 matches b2
  • n=6 matches f3
  • n=7 matches b3
  • n=8 matches f1r2
  • n=9 matches f3+
  • n=10 matches b3x#

I can get the n=1 match to return the proper value using bfsd(?=[bfsd]) and have tried to get the subsequent values to return using lookahead, but can't find a regex which works. Any help is appreciated.


Solution

  • Your item pattern is [bfsd][^bfsd]*.

    You may use ^(?:.*?([bfsd][^bfsd]*)){n} to get what you need, just update the n variable with the number you need to get.

    This pattern will get you the second value:

    ^(?:.*?([bfsd][^bfsd]*)){2}
    

    See regex demo.

    Details

    • ^ - start of string
    • (?:.*?([bfsd][^bfsd]*)){2} - two occurrences of
      • .*? - any 0+ chars, as few as possible
      • ([bfsd][^bfsd]*) - b, f, s or d followed with 0+ chars othet than b, f, s and d.