简体   繁体   中英

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.

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(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

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

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
)
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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