I need to replace this :VARCHAR_1 with e.g. from this string: ‘if :VARCHAR_1 = "1"'. number 1, and if after the colon there is something like :P4_VARCHAR_1 (if it is actually a page item), then just ignore it.
I have already made several of these regexes online through the online builder, but for some reason the rules for Oracle don’t seem to work…
Here is an example regex that should actually work:
select REGEXP_REPLACE('if :P4_VARCHAR_1 = "1"', ':([^P]|P[^\d_])(\w+)', '1') from dual;
Here I expect nothing to change, output: if :P4_VARCHAR_1 = "1"
And for this case:
select REGEXP_REPLACE('if :VARCHAR_1 = "1"', ':([^P]|P[^\d_])(\w+)', '1') from dual;
output: if 1 = "1" so how to make this work in oracle
>Solution :
In Oracle, [] character lists do not support PERL-style character classes in the character lists and they are interpreted as their literal characters. So [^\d_] would match any character that was not \ or d or _.
You want to either use the range 0-9:
SELECT REGEXP_REPLACE(
value,
':([^P]|P[^0-9_])(\w+)',
'1'
) AS output
FROM table_name;
Or a POSIX-style character class:
SELECT REGEXP_REPLACE(
value,
':([^P]|P[^[:digit:]_])(\w+)',
'1'
) AS output
FROM table_name;
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT 'if :P4_VARCHAR_1 = "1"' FROM DUAL UNION ALL
SELECT 'if :VARCHAR_1 = "1"' FROM DUAL
Both output:
| OUTPUT |
|---|
| if :P4_VARCHAR_1 = "1" |
| if 1 = "1" |