简体   繁体   English

使用正则表达式Oracle

[英]Using Regular Expression Oracle

I am looking to use a regular expression to capture the last value in a string. 我正在寻找使用正则表达式来捕获字符串中的最后一个值。 I have copied an example of the data I am looking to parse. 我复制了我要解析的数据的示例。 I am using oracle syntax. 我正在使用oracle语法。

Example Data: 示例数据:

||CULTURE|D0799|D0799HTT|
||CULTURE|D0799|D0799HTT||

I am looking to strip out the last value before the last set of pipes: 我正在寻找最后一组管道之前的最后一个值:

D0799HQTT 
D0799HQTT

I am able to create a regexp_substr that returns the CULTURE: 我能够创建一个regexp_substr返回文化:

REGEXP_SUBSTR(c.field_name, '|[^|]+|')

but I have not been able to figure out how to start at the end look for either one or two pipes, and return the values I'm looking for. 但我一直无法弄清楚如何从头开始寻找一个或两个管道,并返回我要寻找的值。 Let me know if you need more information. 如果您需要更多信息,请与我们联系。

You can try this: 您可以尝试以下方法:

select rtrim(regexp_substr('||CULTURE|D0799|D0799HTT||', 
                     '[[:alnum:]]+\|+$'), '|')
from dual;

Or like this: 或像这样:

select regexp_replace('||CULTURE|D0799|D0799HTT||', 
                     '(^|.*\|)([[:alnum:]]+)\|+$', '\2')
from dual;

Here is a sqlfiddle demo 这是一个sqlfiddle演示

最好拔出不是管道的所有字符,而不是假定它们适合特定的字符集:

select regexp_replace(regexp_substr('||CULTURE SUBS|INTERNATIONAL TESTING S.A.|ISLAND TEST_PEACE|', '[^|]+\|+$'), '\|', '') from dual;

Consider the following Regex... 考虑以下正则表达式...

(?<=\|)[\w\d]*?(?=\|*$)

Good Luck! 祝好运!

SELECT  REPLACE
        (
            REGEXP_SUBSTR(str, '\w+\W{0,}$')
        ,   '|'
        ) AS result
FROM
(
        SELECT  '||CULTURE|D0799|D0799HTT|'  AS str FROM DUAL UNION ALL
        SELECT  '||CULTURE|D0799|D0799HTT||' AS str FROM DUAL
)
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM