Is it possible to use regex_replace_all instead of tl_replace_all
I have the following macro definition:
lethlORIGhl
ExplSyntaxOn
tl_new:N l_jdhao_hlx_tl
RenewDocumentCommandhl{m}{%
tl_set:Nnl_jdhao_hlx_tl{#1}%
tl_replace_all:Nnnl_jdhao_hlx_tl{ }{~}%
tl_replace_all:Nnnl_jdhao_hlx_tl{-/-}{mbox{-/-}}%
tl_replace_all:Nnnl_jdhao_hlx_tl{,}{,{-}}% allow line breaking at a comma
exp_args:NVhlORIGl_jdhao_hlx_tl}%
ExplSyntaxOff
The above does simple replacement, but I don't think that I can do some conditional replacement. For example, I'd like to be able to replace a hyphen that is preceeded and followed by an alphanumeric to be the same, except that the hyphen is replaced by -/ (e.g., alpha-baker would be be changed to alpha-/baker). At the same time where there is a hyphen that either has spaces or non-alphanumeric character will be left alone. Is this possible? What I'm looking to do is have a hyphen that will not cause hyphenation. I'm think that I can use regex_replace_all instead of tl-replace_all. Does this even make sense, or is there a better way?
pdftex pandoc l3regex
add a comment |
I have the following macro definition:
lethlORIGhl
ExplSyntaxOn
tl_new:N l_jdhao_hlx_tl
RenewDocumentCommandhl{m}{%
tl_set:Nnl_jdhao_hlx_tl{#1}%
tl_replace_all:Nnnl_jdhao_hlx_tl{ }{~}%
tl_replace_all:Nnnl_jdhao_hlx_tl{-/-}{mbox{-/-}}%
tl_replace_all:Nnnl_jdhao_hlx_tl{,}{,{-}}% allow line breaking at a comma
exp_args:NVhlORIGl_jdhao_hlx_tl}%
ExplSyntaxOff
The above does simple replacement, but I don't think that I can do some conditional replacement. For example, I'd like to be able to replace a hyphen that is preceeded and followed by an alphanumeric to be the same, except that the hyphen is replaced by -/ (e.g., alpha-baker would be be changed to alpha-/baker). At the same time where there is a hyphen that either has spaces or non-alphanumeric character will be left alone. Is this possible? What I'm looking to do is have a hyphen that will not cause hyphenation. I'm think that I can use regex_replace_all instead of tl-replace_all. Does this even make sense, or is there a better way?
pdftex pandoc l3regex
regex_replace_all:nnNis slower thantl_replace_all:Nnn, but can be used for this, yes.
– Skillmon
2 hours ago
add a comment |
I have the following macro definition:
lethlORIGhl
ExplSyntaxOn
tl_new:N l_jdhao_hlx_tl
RenewDocumentCommandhl{m}{%
tl_set:Nnl_jdhao_hlx_tl{#1}%
tl_replace_all:Nnnl_jdhao_hlx_tl{ }{~}%
tl_replace_all:Nnnl_jdhao_hlx_tl{-/-}{mbox{-/-}}%
tl_replace_all:Nnnl_jdhao_hlx_tl{,}{,{-}}% allow line breaking at a comma
exp_args:NVhlORIGl_jdhao_hlx_tl}%
ExplSyntaxOff
The above does simple replacement, but I don't think that I can do some conditional replacement. For example, I'd like to be able to replace a hyphen that is preceeded and followed by an alphanumeric to be the same, except that the hyphen is replaced by -/ (e.g., alpha-baker would be be changed to alpha-/baker). At the same time where there is a hyphen that either has spaces or non-alphanumeric character will be left alone. Is this possible? What I'm looking to do is have a hyphen that will not cause hyphenation. I'm think that I can use regex_replace_all instead of tl-replace_all. Does this even make sense, or is there a better way?
pdftex pandoc l3regex
I have the following macro definition:
lethlORIGhl
ExplSyntaxOn
tl_new:N l_jdhao_hlx_tl
RenewDocumentCommandhl{m}{%
tl_set:Nnl_jdhao_hlx_tl{#1}%
tl_replace_all:Nnnl_jdhao_hlx_tl{ }{~}%
tl_replace_all:Nnnl_jdhao_hlx_tl{-/-}{mbox{-/-}}%
tl_replace_all:Nnnl_jdhao_hlx_tl{,}{,{-}}% allow line breaking at a comma
exp_args:NVhlORIGl_jdhao_hlx_tl}%
ExplSyntaxOff
The above does simple replacement, but I don't think that I can do some conditional replacement. For example, I'd like to be able to replace a hyphen that is preceeded and followed by an alphanumeric to be the same, except that the hyphen is replaced by -/ (e.g., alpha-baker would be be changed to alpha-/baker). At the same time where there is a hyphen that either has spaces or non-alphanumeric character will be left alone. Is this possible? What I'm looking to do is have a hyphen that will not cause hyphenation. I'm think that I can use regex_replace_all instead of tl-replace_all. Does this even make sense, or is there a better way?
pdftex pandoc l3regex
pdftex pandoc l3regex
asked 3 hours ago
JonBelanger
3910
3910
regex_replace_all:nnNis slower thantl_replace_all:Nnn, but can be used for this, yes.
– Skillmon
2 hours ago
add a comment |
regex_replace_all:nnNis slower thantl_replace_all:Nnn, but can be used for this, yes.
– Skillmon
2 hours ago
regex_replace_all:nnN is slower than tl_replace_all:Nnn, but can be used for this, yes.– Skillmon
2 hours ago
regex_replace_all:nnN is slower than tl_replace_all:Nnn, but can be used for this, yes.– Skillmon
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
A single regex replacing the case you explicitly mentioned in your question (alphanumeric followed by hyphen followed by alphanumeric replaced with the same alphanumeric characters and a -/ in between).
I precompiled the regex with regex_const:Nn, this should save some time if the regex is to be used several times. The code doesn't produce any printed output, only some console ouput:
documentclass{article}
usepackage{expl3}
usepackage{xparse}
ExplSyntaxOn
regex_const:Nn c_jonbelanger_regex { ([A-Za-zd])-([A-Za-zd]) }
tl_new:N l_jonbelanger_tl
NewDocumentCommand hyphenthingy { m }
{
tl_set:Nn l_jonbelanger_tl { #1 }
regex_replace_all:NnN c_jonbelanger_regex { 1c{-}/2 } l_jonbelanger_tl
tl_show_analysis:N l_jonbelanger_tl
}
ExplSyntaxOff
begin{document}
hyphenthingy{alpha-baker}
end{document}
The following is printed to the console:
The token list l_jonbelanger_tl contains the tokens:
> a (the letter a)
> l (the letter l)
> p (the letter p)
> h (the letter h)
> a (the letter a)
> - (control sequence=macro:->x@protect -protect - )
> / (the character /)
> b (the letter b)
> a (the letter a)
> k (the letter k)
> e (the letter e)
> r (the letter r).
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f468336%2fis-it-possible-to-use-regex-replace-all-instead-of-tl-replace-all%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
A single regex replacing the case you explicitly mentioned in your question (alphanumeric followed by hyphen followed by alphanumeric replaced with the same alphanumeric characters and a -/ in between).
I precompiled the regex with regex_const:Nn, this should save some time if the regex is to be used several times. The code doesn't produce any printed output, only some console ouput:
documentclass{article}
usepackage{expl3}
usepackage{xparse}
ExplSyntaxOn
regex_const:Nn c_jonbelanger_regex { ([A-Za-zd])-([A-Za-zd]) }
tl_new:N l_jonbelanger_tl
NewDocumentCommand hyphenthingy { m }
{
tl_set:Nn l_jonbelanger_tl { #1 }
regex_replace_all:NnN c_jonbelanger_regex { 1c{-}/2 } l_jonbelanger_tl
tl_show_analysis:N l_jonbelanger_tl
}
ExplSyntaxOff
begin{document}
hyphenthingy{alpha-baker}
end{document}
The following is printed to the console:
The token list l_jonbelanger_tl contains the tokens:
> a (the letter a)
> l (the letter l)
> p (the letter p)
> h (the letter h)
> a (the letter a)
> - (control sequence=macro:->x@protect -protect - )
> / (the character /)
> b (the letter b)
> a (the letter a)
> k (the letter k)
> e (the letter e)
> r (the letter r).
add a comment |
A single regex replacing the case you explicitly mentioned in your question (alphanumeric followed by hyphen followed by alphanumeric replaced with the same alphanumeric characters and a -/ in between).
I precompiled the regex with regex_const:Nn, this should save some time if the regex is to be used several times. The code doesn't produce any printed output, only some console ouput:
documentclass{article}
usepackage{expl3}
usepackage{xparse}
ExplSyntaxOn
regex_const:Nn c_jonbelanger_regex { ([A-Za-zd])-([A-Za-zd]) }
tl_new:N l_jonbelanger_tl
NewDocumentCommand hyphenthingy { m }
{
tl_set:Nn l_jonbelanger_tl { #1 }
regex_replace_all:NnN c_jonbelanger_regex { 1c{-}/2 } l_jonbelanger_tl
tl_show_analysis:N l_jonbelanger_tl
}
ExplSyntaxOff
begin{document}
hyphenthingy{alpha-baker}
end{document}
The following is printed to the console:
The token list l_jonbelanger_tl contains the tokens:
> a (the letter a)
> l (the letter l)
> p (the letter p)
> h (the letter h)
> a (the letter a)
> - (control sequence=macro:->x@protect -protect - )
> / (the character /)
> b (the letter b)
> a (the letter a)
> k (the letter k)
> e (the letter e)
> r (the letter r).
add a comment |
A single regex replacing the case you explicitly mentioned in your question (alphanumeric followed by hyphen followed by alphanumeric replaced with the same alphanumeric characters and a -/ in between).
I precompiled the regex with regex_const:Nn, this should save some time if the regex is to be used several times. The code doesn't produce any printed output, only some console ouput:
documentclass{article}
usepackage{expl3}
usepackage{xparse}
ExplSyntaxOn
regex_const:Nn c_jonbelanger_regex { ([A-Za-zd])-([A-Za-zd]) }
tl_new:N l_jonbelanger_tl
NewDocumentCommand hyphenthingy { m }
{
tl_set:Nn l_jonbelanger_tl { #1 }
regex_replace_all:NnN c_jonbelanger_regex { 1c{-}/2 } l_jonbelanger_tl
tl_show_analysis:N l_jonbelanger_tl
}
ExplSyntaxOff
begin{document}
hyphenthingy{alpha-baker}
end{document}
The following is printed to the console:
The token list l_jonbelanger_tl contains the tokens:
> a (the letter a)
> l (the letter l)
> p (the letter p)
> h (the letter h)
> a (the letter a)
> - (control sequence=macro:->x@protect -protect - )
> / (the character /)
> b (the letter b)
> a (the letter a)
> k (the letter k)
> e (the letter e)
> r (the letter r).
A single regex replacing the case you explicitly mentioned in your question (alphanumeric followed by hyphen followed by alphanumeric replaced with the same alphanumeric characters and a -/ in between).
I precompiled the regex with regex_const:Nn, this should save some time if the regex is to be used several times. The code doesn't produce any printed output, only some console ouput:
documentclass{article}
usepackage{expl3}
usepackage{xparse}
ExplSyntaxOn
regex_const:Nn c_jonbelanger_regex { ([A-Za-zd])-([A-Za-zd]) }
tl_new:N l_jonbelanger_tl
NewDocumentCommand hyphenthingy { m }
{
tl_set:Nn l_jonbelanger_tl { #1 }
regex_replace_all:NnN c_jonbelanger_regex { 1c{-}/2 } l_jonbelanger_tl
tl_show_analysis:N l_jonbelanger_tl
}
ExplSyntaxOff
begin{document}
hyphenthingy{alpha-baker}
end{document}
The following is printed to the console:
The token list l_jonbelanger_tl contains the tokens:
> a (the letter a)
> l (the letter l)
> p (the letter p)
> h (the letter h)
> a (the letter a)
> - (control sequence=macro:->x@protect -protect - )
> / (the character /)
> b (the letter b)
> a (the letter a)
> k (the letter k)
> e (the letter e)
> r (the letter r).
answered 2 hours ago
Skillmon
21.1k11941
21.1k11941
add a comment |
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%2f468336%2fis-it-possible-to-use-regex-replace-all-instead-of-tl-replace-all%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
regex_replace_all:nnNis slower thantl_replace_all:Nnn, but can be used for this, yes.– Skillmon
2 hours ago