Domanda

Using owa_pattern.change in oracle 9i.

Is it possible to remove a number and the trailing special character (pls note only the trailing) special character in a string?

I refer to special character as characters that is neither a word nor a number. e.g _ , # , @ ,$ etc ...

For example.

String = TEST_STRING_10

desired output would be TEST_STRING (notice only the trailing special character _ was removed).

I have already figured out how to remove the number but is stuck in the special character part.

I have this code so far.

OWA_PATTERN.CHANGE (string, '\d', '', 'g');

Appreciate any inputs.

Thanks!

È stato utile?

Soluzione

Try the following.

OWA_PATTERN.CHANGE (string, '[^a-zA-Z]+$', '');

Regular expression

[^a-zA-Z]+    any character except: 'a' to 'z', 'A' to 'Z' 
              (1 or more times (matching the most amount possible))
 $            before an optional \n, and the end of the string

Altri suggerimenti

This will do it:

DECLARE
    result VARCHAR2(255);
BEGIN
    string := 'TEST_STRING_10';
    result := REGEXP_REPLACE(string, '([[:alnum:]_].*)_[[:digit:]]+', '\1', 1, 0, 'c');
END;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top