Find source na replacement parameters in a string and perform replacement in another string
I need a function that applies a certain rule to replace words in a string.
There are two variables:
- v_imp_user_list - list of users delimited by pipe (example: JOHN|PETER|MARK|USER_PROD)
- v_schema_remap_list - list of users that should be remapped (old value - new value, example: JOHN-GEORGE,USER_PROD-USER_TEST)
The function should parse v_schema_remap_list variable and if the name of the first user (before dash, old value)
exist in v_imp_user_list then replace it with the second user (after dash, new value).
Example:
v_imp_user_list := 'JOHN|PETER|MARK|USER_PROD'
v_schema_remap_list := 'JOHN-GEORGE,USER_PROD-USER_TEST'
Desired outcome:
GEORGE|PETER|MARK|USER_TEST
I have a solution which I will post but for some reason I don't like it and will appreciate any comment/review/better solution.
oracle function replace plsql regexp-replace
add a comment |
I need a function that applies a certain rule to replace words in a string.
There are two variables:
- v_imp_user_list - list of users delimited by pipe (example: JOHN|PETER|MARK|USER_PROD)
- v_schema_remap_list - list of users that should be remapped (old value - new value, example: JOHN-GEORGE,USER_PROD-USER_TEST)
The function should parse v_schema_remap_list variable and if the name of the first user (before dash, old value)
exist in v_imp_user_list then replace it with the second user (after dash, new value).
Example:
v_imp_user_list := 'JOHN|PETER|MARK|USER_PROD'
v_schema_remap_list := 'JOHN-GEORGE,USER_PROD-USER_TEST'
Desired outcome:
GEORGE|PETER|MARK|USER_TEST
I have a solution which I will post but for some reason I don't like it and will appreciate any comment/review/better solution.
oracle function replace plsql regexp-replace
add a comment |
I need a function that applies a certain rule to replace words in a string.
There are two variables:
- v_imp_user_list - list of users delimited by pipe (example: JOHN|PETER|MARK|USER_PROD)
- v_schema_remap_list - list of users that should be remapped (old value - new value, example: JOHN-GEORGE,USER_PROD-USER_TEST)
The function should parse v_schema_remap_list variable and if the name of the first user (before dash, old value)
exist in v_imp_user_list then replace it with the second user (after dash, new value).
Example:
v_imp_user_list := 'JOHN|PETER|MARK|USER_PROD'
v_schema_remap_list := 'JOHN-GEORGE,USER_PROD-USER_TEST'
Desired outcome:
GEORGE|PETER|MARK|USER_TEST
I have a solution which I will post but for some reason I don't like it and will appreciate any comment/review/better solution.
oracle function replace plsql regexp-replace
I need a function that applies a certain rule to replace words in a string.
There are two variables:
- v_imp_user_list - list of users delimited by pipe (example: JOHN|PETER|MARK|USER_PROD)
- v_schema_remap_list - list of users that should be remapped (old value - new value, example: JOHN-GEORGE,USER_PROD-USER_TEST)
The function should parse v_schema_remap_list variable and if the name of the first user (before dash, old value)
exist in v_imp_user_list then replace it with the second user (after dash, new value).
Example:
v_imp_user_list := 'JOHN|PETER|MARK|USER_PROD'
v_schema_remap_list := 'JOHN-GEORGE,USER_PROD-USER_TEST'
Desired outcome:
GEORGE|PETER|MARK|USER_TEST
I have a solution which I will post but for some reason I don't like it and will appreciate any comment/review/better solution.
oracle function replace plsql regexp-replace
oracle function replace plsql regexp-replace
asked Nov 15 '18 at 6:36
KaipanKaipan
274
274
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This function helps me to parse the v_schema_remap_list.
-- extract nth occurence in a delimited string
create or replace function f_find_str (
source_string varchar2
, occurence_outer number --occurence of the old-new pair
, occurence_inner number --old/new value, enter 1 for old or 2 for new
)
return varchar2
is
v_aux varchar2(500);
v_result varchar2(100);
begin
v_aux := ltrim(regexp_substr(',' || source_string, ',[^,]*', 1, occurence_outer),',');
if occurence_inner = 1 then
v_result := substr(v_aux, 1, instr(v_aux, '-')-1);
elsif occurence_inner = 2 then
v_result := substr(v_aux, instr(v_aux, '-')+1);
end if;
return v_result;
end;
/
This anonymous block shows my solution (see the loop inside).
declare
v_schema_remap_list varchar2(1000) := 'JOHN-GEORGE,USER_PROD-USER_TEST';
v_imp_user_list varchar2(1000) := 'JOHN|PETER|MARK|USER_PROD';
v_schema_remap_cnt number := nvl(regexp_count(v_schema_remap_list, '-'), 0);
begin
for i in 1..v_schema_remap_cnt
loop
v_imp_user_list := replace(
v_imp_user_list||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 1
)||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 2
)||'|'
);
v_imp_user_list := rtrim(v_imp_user_list, '|');
end loop;
dbms_output.put_line(v_imp_user_list); --test output
end;
/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313722%2ffind-source-na-replacement-parameters-in-a-string-and-perform-replacement-in-ano%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This function helps me to parse the v_schema_remap_list.
-- extract nth occurence in a delimited string
create or replace function f_find_str (
source_string varchar2
, occurence_outer number --occurence of the old-new pair
, occurence_inner number --old/new value, enter 1 for old or 2 for new
)
return varchar2
is
v_aux varchar2(500);
v_result varchar2(100);
begin
v_aux := ltrim(regexp_substr(',' || source_string, ',[^,]*', 1, occurence_outer),',');
if occurence_inner = 1 then
v_result := substr(v_aux, 1, instr(v_aux, '-')-1);
elsif occurence_inner = 2 then
v_result := substr(v_aux, instr(v_aux, '-')+1);
end if;
return v_result;
end;
/
This anonymous block shows my solution (see the loop inside).
declare
v_schema_remap_list varchar2(1000) := 'JOHN-GEORGE,USER_PROD-USER_TEST';
v_imp_user_list varchar2(1000) := 'JOHN|PETER|MARK|USER_PROD';
v_schema_remap_cnt number := nvl(regexp_count(v_schema_remap_list, '-'), 0);
begin
for i in 1..v_schema_remap_cnt
loop
v_imp_user_list := replace(
v_imp_user_list||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 1
)||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 2
)||'|'
);
v_imp_user_list := rtrim(v_imp_user_list, '|');
end loop;
dbms_output.put_line(v_imp_user_list); --test output
end;
/
add a comment |
This function helps me to parse the v_schema_remap_list.
-- extract nth occurence in a delimited string
create or replace function f_find_str (
source_string varchar2
, occurence_outer number --occurence of the old-new pair
, occurence_inner number --old/new value, enter 1 for old or 2 for new
)
return varchar2
is
v_aux varchar2(500);
v_result varchar2(100);
begin
v_aux := ltrim(regexp_substr(',' || source_string, ',[^,]*', 1, occurence_outer),',');
if occurence_inner = 1 then
v_result := substr(v_aux, 1, instr(v_aux, '-')-1);
elsif occurence_inner = 2 then
v_result := substr(v_aux, instr(v_aux, '-')+1);
end if;
return v_result;
end;
/
This anonymous block shows my solution (see the loop inside).
declare
v_schema_remap_list varchar2(1000) := 'JOHN-GEORGE,USER_PROD-USER_TEST';
v_imp_user_list varchar2(1000) := 'JOHN|PETER|MARK|USER_PROD';
v_schema_remap_cnt number := nvl(regexp_count(v_schema_remap_list, '-'), 0);
begin
for i in 1..v_schema_remap_cnt
loop
v_imp_user_list := replace(
v_imp_user_list||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 1
)||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 2
)||'|'
);
v_imp_user_list := rtrim(v_imp_user_list, '|');
end loop;
dbms_output.put_line(v_imp_user_list); --test output
end;
/
add a comment |
This function helps me to parse the v_schema_remap_list.
-- extract nth occurence in a delimited string
create or replace function f_find_str (
source_string varchar2
, occurence_outer number --occurence of the old-new pair
, occurence_inner number --old/new value, enter 1 for old or 2 for new
)
return varchar2
is
v_aux varchar2(500);
v_result varchar2(100);
begin
v_aux := ltrim(regexp_substr(',' || source_string, ',[^,]*', 1, occurence_outer),',');
if occurence_inner = 1 then
v_result := substr(v_aux, 1, instr(v_aux, '-')-1);
elsif occurence_inner = 2 then
v_result := substr(v_aux, instr(v_aux, '-')+1);
end if;
return v_result;
end;
/
This anonymous block shows my solution (see the loop inside).
declare
v_schema_remap_list varchar2(1000) := 'JOHN-GEORGE,USER_PROD-USER_TEST';
v_imp_user_list varchar2(1000) := 'JOHN|PETER|MARK|USER_PROD';
v_schema_remap_cnt number := nvl(regexp_count(v_schema_remap_list, '-'), 0);
begin
for i in 1..v_schema_remap_cnt
loop
v_imp_user_list := replace(
v_imp_user_list||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 1
)||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 2
)||'|'
);
v_imp_user_list := rtrim(v_imp_user_list, '|');
end loop;
dbms_output.put_line(v_imp_user_list); --test output
end;
/
This function helps me to parse the v_schema_remap_list.
-- extract nth occurence in a delimited string
create or replace function f_find_str (
source_string varchar2
, occurence_outer number --occurence of the old-new pair
, occurence_inner number --old/new value, enter 1 for old or 2 for new
)
return varchar2
is
v_aux varchar2(500);
v_result varchar2(100);
begin
v_aux := ltrim(regexp_substr(',' || source_string, ',[^,]*', 1, occurence_outer),',');
if occurence_inner = 1 then
v_result := substr(v_aux, 1, instr(v_aux, '-')-1);
elsif occurence_inner = 2 then
v_result := substr(v_aux, instr(v_aux, '-')+1);
end if;
return v_result;
end;
/
This anonymous block shows my solution (see the loop inside).
declare
v_schema_remap_list varchar2(1000) := 'JOHN-GEORGE,USER_PROD-USER_TEST';
v_imp_user_list varchar2(1000) := 'JOHN|PETER|MARK|USER_PROD';
v_schema_remap_cnt number := nvl(regexp_count(v_schema_remap_list, '-'), 0);
begin
for i in 1..v_schema_remap_cnt
loop
v_imp_user_list := replace(
v_imp_user_list||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 1
)||'|',
f_find_str (
source_string => v_schema_remap_list
, occurence_outer => i
, occurence_inner => 2
)||'|'
);
v_imp_user_list := rtrim(v_imp_user_list, '|');
end loop;
dbms_output.put_line(v_imp_user_list); --test output
end;
/
answered Nov 15 '18 at 6:40
KaipanKaipan
274
274
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313722%2ffind-source-na-replacement-parameters-in-a-string-and-perform-replacement-in-ano%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown