Regex- Extract occurrence between to delimiters

2k views Asked by At

How can I extract subsequent occurrence in this pattern:

123|xx3|1x23|1x2x3|x123x|123x

So for example I want to extract 3rd occurrence only, I've tried to use:

(?:.*?\\|){3}(.*)

But it extracts from 3rd occurrence and everything after it.

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

The REGEXP_EXTRACT reference shows an example of using a capturing group to extract the portion of a string you need.

So, match up to the 3rd part, and then capture the 3rd value:

^(?:[^|]*[|]){2}([^|]*)

Here is a demo, the green highlighted part will get extracted.

Details

  • ^ - start of string
  • (?:[^|]*[|]){2} - 2 occurrences of:
    • [^|]* - zero or more chars other than |
    • [|] - a | pipe symbol
  • ([^|]*) - Group 1: zero or more chars other than |.