正则表达式提取由管道分隔的字符串的第n个令牌 [英] Regex to extract nth token of a string separated by pipes
问题描述
我是Regex的新手
我需要从以下示例文本中计数和提取令牌:
I need to count and extract tokens from the below sample text:
AA||CCCCCCCC|||FFFFFFFFFFF
请求第四名令牌我必须得到一个空的''字符串,要求第六个我必须得到'FFFFFFFFFFF'
Requesting 4th token I must get a empty '' string, requesting 6th I must get 'FFFFFFFFFFF'
是否可以有这样的正则表达式?
Would it be possible to have such regex?
预先感谢!
PS:对于代币计数,我使用了'\ |'
向结果中添加+1是字符串不为空,肯定还有另一种更有效的方法,仅使用正则表达式...
PS: For token counting I've used '\|'
adding +1 to the result is the string is not empty, surely there another more efficient way to do that using just a regex...
推荐答案
对于DB2,请尝试此操作以获取列表中的第6个元素。这适用于Oracle,并且允许使用NULL列表元素。 REGEXP_SUBSTR调用的语法是相同的,因此我怀疑它会起作用:
For DB2 please try this to get the 6th element in the list. This works on Oracle and allows for NULL list elements. The syntax for the REGEXP_SUBSTR call is the same so I suspect it will work:
regexp_substr('AA||CCCCCCCC|||FFFFFFFFFFF', '(.*?)(\||$)', 1, 6, 'c', 1)
编辑: c表示区分大小写
'c' for case-sensitive
这篇关于正则表达式提取由管道分隔的字符串的第n个令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!