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}
characters
add a comment |
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}
characters
1
Do you mean combining them as inreplace
which doesreplaceA{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 macroreplaceresult
:replaceB{a/b*cde}
does not typeset but stores inreplaceresult
.expandafterreplaceAexpandafter{replaceresult}
will now act on that and the newreplaceresult
is what you want. you can also define them to expand once, so you don't needexpandafter
. Also, simplest might be to define a third commandreplaceC
doing all your replacements at once, if you want to stick to your initial way of doing things
– jfbu
Nov 11 at 11:00
add a comment |
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}
characters
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
characters
edited Nov 11 at 9:01
asked Nov 11 at 8:19
Ashok
367211
367211
1
Do you mean combining them as inreplace
which doesreplaceA{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 macroreplaceresult
:replaceB{a/b*cde}
does not typeset but stores inreplaceresult
.expandafterreplaceAexpandafter{replaceresult}
will now act on that and the newreplaceresult
is what you want. you can also define them to expand once, so you don't needexpandafter
. Also, simplest might be to define a third commandreplaceC
doing all your replacements at once, if you want to stick to your initial way of doing things
– jfbu
Nov 11 at 11:00
add a comment |
1
Do you mean combining them as inreplace
which doesreplaceA{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 macroreplaceresult
:replaceB{a/b*cde}
does not typeset but stores inreplaceresult
.expandafterreplaceAexpandafter{replaceresult}
will now act on that and the newreplaceresult
is what you want. you can also define them to expand once, so you don't needexpandafter
. Also, simplest might be to define a third commandreplaceC
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
add a comment |
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}
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
add a comment |
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}
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
add a comment |
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}
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
add a comment |
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}
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}
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
add a comment |
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
add a comment |
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.
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%2ftex.stackexchange.com%2fquestions%2f459429%2freplacing-multiple-characters-including-backslash%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
1
Do you mean combining them as in
replace
which doesreplaceA{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 inreplaceresult
.expandafterreplaceAexpandafter{replaceresult}
will now act on that and the newreplaceresult
is what you want. you can also define them to expand once, so you don't needexpandafter
. Also, simplest might be to define a third commandreplaceC
doing all your replacements at once, if you want to stick to your initial way of doing things– jfbu
Nov 11 at 11:00