Search code examples
regexoracle-databasesubstr

Oracle REGEXP_SUBSTR - SEMICOLON STRING EXTRACTION


I have below input and need mentioned output. How I can get it. I tried different pattern but could not get through it.

so in brief, any value having all three 1#2#3 parts(if it is present) or first value should be returned

2#9#;2#37#65 ->  2#37#65
2#9#;2#37#65;2#37#  -> 2#37#65 
2#9#;2#37#65;2#37#;2#37#56  -> 2#37#65 or 2#37#56
2#37#65;2#99  -> 2#37#65
3#9#;3#37#65;3#37#36;2#37#56  -> 3#37#65 or 3#37#36 or 2#37#56
2#37#;2#99#  -> 2#37 or 2#99# ( in this case any value)

I tried few patterns and other pattern but no help.

    regexp_substr('2#9#;2#37#65;2#37#','#[^;]+',1)
    SUBSTR(REGEXP_SUBSTR(SUBSTR(uo_filiere,1,INSTR(uo_filiere,';',1)-1), '#[^#]+$'),2)

Solution

  • You can use a REGEXP_REPLACE here:

    REGEXP_REPLACE(uo_filiere, '^(.*;)?([0-9]+(#[0-9]+){2,}).*|^([^;]+).*', '\2\4')
    

    See the regexp demo

    Details:

    • ^ - start of string
    • (.*;)? - an optional Group 1 capturing any text and then a ;
    • ([0-9]+(#[0-9]+){2,}) - Group 2 (\2): one or more digits, and then two or more occurrences of # followed with one or more digits
    • .* - the rest of the string
    • | - or
    • ^([^;]+).* - start of string, Group 4 capturing one or more chars other than ; and then any text till end of string.

    The replacement is Group 2 + Group 4 values.