Replacing Multiple Characters Including Backslash











up vote
5
down vote

favorite












I am in a situation to replace many characters from a sentence. In the working example attached below, command relaceB replaces backslash and command replaceA replaces many other characters like **, /* etc. But I am unable to combine both the commands together to form a single new command (which replaces- *,/, etc). Does anybody know how to fix it?



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
tl_replace_all:Nnn l_azor_string_tl { / } { -- }
tl_replace_all:Nnn l_azor_string_tl { ? } { -- }
tl_replace_all:Nnn l_azor_string_tl { * } { --}
tl_use:N l_azor_string_tl
}
NewDocumentCommand replaceA { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

%Removing backslash
ExplSyntaxOn
NewDocumentCommand{replaceB}{m}
{
tl_set:Nn l_tmpa_tl { #1 }
regex_replace_all:nnN { cC. } { c{cs_to_str:N} } l_tmpa_tl
tl_use:N l_tmpa_tl
}
ExplSyntaxOff

begin{document}
replaceA{a/b*c} %Works

replaceB{de} %Works

replaceA{replaceB{a/b*cde}} %Does not work
end{document}









share|improve this question




















  • 1




    Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
    – TeXnician
    Nov 11 at 8:43










  • @TeXnician, yes, a command which works in a nested way would be more suitable.
    – Ashok
    Nov 11 at 8:55












  • @TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
    – Ashok
    Nov 11 at 8:57






  • 1




    These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
    – jfbu
    Nov 11 at 11:00















up vote
5
down vote

favorite












I am in a situation to replace many characters from a sentence. In the working example attached below, command relaceB replaces backslash and command replaceA replaces many other characters like **, /* etc. But I am unable to combine both the commands together to form a single new command (which replaces- *,/, etc). Does anybody know how to fix it?



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
tl_replace_all:Nnn l_azor_string_tl { / } { -- }
tl_replace_all:Nnn l_azor_string_tl { ? } { -- }
tl_replace_all:Nnn l_azor_string_tl { * } { --}
tl_use:N l_azor_string_tl
}
NewDocumentCommand replaceA { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

%Removing backslash
ExplSyntaxOn
NewDocumentCommand{replaceB}{m}
{
tl_set:Nn l_tmpa_tl { #1 }
regex_replace_all:nnN { cC. } { c{cs_to_str:N} } l_tmpa_tl
tl_use:N l_tmpa_tl
}
ExplSyntaxOff

begin{document}
replaceA{a/b*c} %Works

replaceB{de} %Works

replaceA{replaceB{a/b*cde}} %Does not work
end{document}









share|improve this question




















  • 1




    Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
    – TeXnician
    Nov 11 at 8:43










  • @TeXnician, yes, a command which works in a nested way would be more suitable.
    – Ashok
    Nov 11 at 8:55












  • @TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
    – Ashok
    Nov 11 at 8:57






  • 1




    These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
    – jfbu
    Nov 11 at 11:00













up vote
5
down vote

favorite









up vote
5
down vote

favorite











I am in a situation to replace many characters from a sentence. In the working example attached below, command relaceB replaces backslash and command replaceA replaces many other characters like **, /* etc. But I am unable to combine both the commands together to form a single new command (which replaces- *,/, etc). Does anybody know how to fix it?



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
tl_replace_all:Nnn l_azor_string_tl { / } { -- }
tl_replace_all:Nnn l_azor_string_tl { ? } { -- }
tl_replace_all:Nnn l_azor_string_tl { * } { --}
tl_use:N l_azor_string_tl
}
NewDocumentCommand replaceA { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

%Removing backslash
ExplSyntaxOn
NewDocumentCommand{replaceB}{m}
{
tl_set:Nn l_tmpa_tl { #1 }
regex_replace_all:nnN { cC. } { c{cs_to_str:N} } l_tmpa_tl
tl_use:N l_tmpa_tl
}
ExplSyntaxOff

begin{document}
replaceA{a/b*c} %Works

replaceB{de} %Works

replaceA{replaceB{a/b*cde}} %Does not work
end{document}









share|improve this question















I am in a situation to replace many characters from a sentence. In the working example attached below, command relaceB replaces backslash and command replaceA replaces many other characters like **, /* etc. But I am unable to combine both the commands together to form a single new command (which replaces- *,/, etc). Does anybody know how to fix it?



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
tl_replace_all:Nnn l_azor_string_tl { / } { -- }
tl_replace_all:Nnn l_azor_string_tl { ? } { -- }
tl_replace_all:Nnn l_azor_string_tl { * } { --}
tl_use:N l_azor_string_tl
}
NewDocumentCommand replaceA { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

%Removing backslash
ExplSyntaxOn
NewDocumentCommand{replaceB}{m}
{
tl_set:Nn l_tmpa_tl { #1 }
regex_replace_all:nnN { cC. } { c{cs_to_str:N} } l_tmpa_tl
tl_use:N l_tmpa_tl
}
ExplSyntaxOff

begin{document}
replaceA{a/b*c} %Works

replaceB{de} %Works

replaceA{replaceB{a/b*cde}} %Does not work
end{document}






characters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:01

























asked Nov 11 at 8:19









Ashok

367211




367211








  • 1




    Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
    – TeXnician
    Nov 11 at 8:43










  • @TeXnician, yes, a command which works in a nested way would be more suitable.
    – Ashok
    Nov 11 at 8:55












  • @TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
    – Ashok
    Nov 11 at 8:57






  • 1




    These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
    – jfbu
    Nov 11 at 11:00














  • 1




    Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
    – TeXnician
    Nov 11 at 8:43










  • @TeXnician, yes, a command which works in a nested way would be more suitable.
    – Ashok
    Nov 11 at 8:55












  • @TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
    – Ashok
    Nov 11 at 8:57






  • 1




    These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
    – jfbu
    Nov 11 at 11:00








1




1




Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
– TeXnician
Nov 11 at 8:43




Do you mean combining them as in replace which does replaceA{replaceB{#1}} or as in you want them to work in this nested way?
– TeXnician
Nov 11 at 8:43












@TeXnician, yes, a command which works in a nested way would be more suitable.
– Ashok
Nov 11 at 8:55






@TeXnician, yes, a command which works in a nested way would be more suitable.
– Ashok
Nov 11 at 8:55














@TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
– Ashok
Nov 11 at 8:57




@TeXnician, in the third line, the output does not contain backslash (only "de") but the other characters (/,*) are not removed.
– Ashok
Nov 11 at 8:57




1




1




These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
– jfbu
Nov 11 at 11:00




These commands are not expandable, and they don't expand their arguments, so they can't be easily nested. You could modify them to store their product in macro replaceresult: replaceB{a/b*cde} does not typeset but stores in replaceresult. expandafterreplaceAexpandafter{replaceresult} will now act on that and the new replaceresult is what you want. you can also define them to expand once, so you don't need expandafter. Also, simplest might be to define a third command replaceC doing all your replacements at once, if you want to stick to your initial way of doing things
– jfbu
Nov 11 at 11:00










1 Answer
1






active

oldest

votes

















up vote
5
down vote













Use regex_replace_all:nnN to do all the business at the same time:



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
regex_replace_all:nnN { cC. } { -- c{cs_to_str:N} } l_azor_string_tl
regex_replace_all:nnN { [/?*] } { -- } l_azor_string_tl
tl_use:N l_azor_string_tl
}
NewDocumentCommand replace { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

begin{document}

replace{a/b*c}

replace{de}

replace{a/b*cde?f*g}

end{document}


enter image description here






share|improve this answer























  • thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
    – Ashok
    Nov 11 at 13:12






  • 1




    @Ashok Fixed so also the backslash becomes --
    – egreg
    Nov 11 at 13:16











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f459429%2freplacing-multiple-characters-including-backslash%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








up vote
5
down vote













Use regex_replace_all:nnN to do all the business at the same time:



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
regex_replace_all:nnN { cC. } { -- c{cs_to_str:N} } l_azor_string_tl
regex_replace_all:nnN { [/?*] } { -- } l_azor_string_tl
tl_use:N l_azor_string_tl
}
NewDocumentCommand replace { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

begin{document}

replace{a/b*c}

replace{de}

replace{a/b*cde?f*g}

end{document}


enter image description here






share|improve this answer























  • thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
    – Ashok
    Nov 11 at 13:12






  • 1




    @Ashok Fixed so also the backslash becomes --
    – egreg
    Nov 11 at 13:16















up vote
5
down vote













Use regex_replace_all:nnN to do all the business at the same time:



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
regex_replace_all:nnN { cC. } { -- c{cs_to_str:N} } l_azor_string_tl
regex_replace_all:nnN { [/?*] } { -- } l_azor_string_tl
tl_use:N l_azor_string_tl
}
NewDocumentCommand replace { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

begin{document}

replace{a/b*c}

replace{de}

replace{a/b*cde?f*g}

end{document}


enter image description here






share|improve this answer























  • thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
    – Ashok
    Nov 11 at 13:12






  • 1




    @Ashok Fixed so also the backslash becomes --
    – egreg
    Nov 11 at 13:16













up vote
5
down vote










up vote
5
down vote









Use regex_replace_all:nnN to do all the business at the same time:



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
regex_replace_all:nnN { cC. } { -- c{cs_to_str:N} } l_azor_string_tl
regex_replace_all:nnN { [/?*] } { -- } l_azor_string_tl
tl_use:N l_azor_string_tl
}
NewDocumentCommand replace { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

begin{document}

replace{a/b*c}

replace{de}

replace{a/b*cde?f*g}

end{document}


enter image description here






share|improve this answer














Use regex_replace_all:nnN to do all the business at the same time:



documentclass{article}
usepackage{xparse}

%Removing other characters
ExplSyntaxOn
tl_new:N l_azor_string_tl
cs_new_protected:Npn azor_replace_norsk:n #1
{
tl_set:Nn l_azor_string_tl { #1 }
regex_replace_all:nnN { cC. } { -- c{cs_to_str:N} } l_azor_string_tl
regex_replace_all:nnN { [/?*] } { -- } l_azor_string_tl
tl_use:N l_azor_string_tl
}
NewDocumentCommand replace { m }
{
azor_replace_norsk:n { #1 }
}
ExplSyntaxOff

begin{document}

replace{a/b*c}

replace{de}

replace{a/b*cde?f*g}

end{document}


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 13:16

























answered Nov 11 at 11:30









egreg

702k8618733147




702k8618733147












  • thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
    – Ashok
    Nov 11 at 13:12






  • 1




    @Ashok Fixed so also the backslash becomes --
    – egreg
    Nov 11 at 13:16


















  • thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
    – Ashok
    Nov 11 at 13:12






  • 1




    @Ashok Fixed so also the backslash becomes --
    – egreg
    Nov 11 at 13:16
















thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
– Ashok
Nov 11 at 13:12




thanks, this works, but the backslash is just removed, not replaced by hyphen (--).
– Ashok
Nov 11 at 13:12




1




1




@Ashok Fixed so also the backslash becomes --
– egreg
Nov 11 at 13:16




@Ashok Fixed so also the backslash becomes --
– egreg
Nov 11 at 13:16


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f459429%2freplacing-multiple-characters-including-backslash%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation