(1) If you just need to extract the first occurrence of such string:
You can use .str.extract(), as follows:
df['Fetch'] = df['Test'].str.extract(r'\|([^|]+)\|')
Regex \|([^|]+)\| is the simplest form you can use:
\| matches the delimiter | before the target string
( opening parenthesis for capturing group
[^|] character class for any character other than the delimiter |
+ quantifier for one or more occurrence(s)
) opening parenthesis for capturing group
\| matches the delimiter | after the target string
Result:
Added 2 more rows for more test cases
print(df)
Test Fetch
0 |||||CHNBAD||POC-RM0EP7-01-A CHNBAD
1 |||||CHNBAD||POC-RM0EP7-01-A| CHNBAD
2 ||ABC||DEF|GHI|||JKL ABC
(2) If you want to extract ALL the occurrences of such strings
You can use .str.extractall(), as follows:
df = df[['Test']].join(df['Test'].str.extractall(r'(?<=\|)([^|]+)(?=\|)').unstack().droplevel(0, axis=1).rename(lambda x: 'Fetch_' + str(x+1), axis=1))
Result:
print(df)
Test Fetch_1 Fetch_2 Fetch_3
0 |||||CHNBAD||POC-RM0EP7-01-A CHNBAD NaN NaN
1 |||||CHNBAD||POC-RM0EP7-01-A| CHNBAD POC-RM0EP7-01-A NaN
2 ||ABC||DEF|GHI|||JKL ABC DEF GHI
Here, we need a more complex regex so as to extract 3 matches from the last row. If we use the previous regex, only 2 matches will be extracted.
If you want more explanation of the regex and codes, I can do it later, upon your request.