3

Can you use l3regex / \regex_replace_all:nnN for replacing each explicit character token of category 11(letter) in a token list by its category 12(other) pendant? Or by its category 6(parameter) pendant?

More generalized: Can you use l3regex / \regex_replace_all:nnN for replacing each explicit character token of a certain category in a token-list by its pendant of another category?
(Leave scenarios aside where this would lead to tokens of the token-list containing unbalanced braces at some time of the processing.)

If so: How? What must the replacement text with \regex_replace_all:nnN look like?

If not: Never mind. I will write my own replacement-routine. ;-)

As expected by me the example below does not work out—\scratchy_showloop:n reveals that after the regexp-replace the token-list still contains "letters"=character tokens of category 11, and not "characters"=character tokens of category 12.

(I deliver this minimal example for satisfying those who insist in seeing a minimal example.)

\ExplSyntaxOn

\tl_new:N \l__scratchy_tl

\cs_new:Nn \scratchy_showloop:n {
  \quark_if_recursion_tail_stop:n {#1}
  \cs_show:N #1
  \scratchy_showloop:n
}

\tl_set:Nn  \l__scratchy_tl { abcdefg }

\regex_replace_all:nnN { \cL. } { \cO(\0) }  \l__scratchy_tl

\exp_args:NnV \use:nn \scratchy_showloop:n  \l__scratchy_tl  \q_recursion_tail \q_recursion_stop

\stop
22
  • 1
    if the tokens in the list are not expandable you can insert \string before each found character then edef the whole list at the end Feb 2, 2022 at 9:01
  • 1
    (for the own-replacement-routine part, tokcycle may help if you(future reader)'re open to new packages. expl3 manipulation of token list that preserves whitespaces is a little confusing (there's \tl_if_head_is_space but none for determining how many spaces there are.)
    – user202729
    Feb 2, 2022 at 9:53
  • @DavidCarlisle So I chose a bad scenario. Instead assume for some obscure reason replacing each explicit character token of category 11(letter) in a token list by its category 6(parameter) pendant. ;-) Feb 2, 2022 at 11:06
  • 1
    @UlrichDiez Bruno has been informed of the problem and said “Hum... indeed it would be good if that would work.” There may be a workable solution in the near future.
    – egreg
    Feb 2, 2022 at 13:42
  • 1
    @PhelypeOleinik It's definitely more my fault than the medium's. Unfortunately, I have a talent for appearing odd. Even people who see me face-to-face say that. Your way of responding to questions and providing answers is characterized by a level of courtesy, objectivity, expertise and diligence that I admire. I am far from taking umbrage at anything you write. :-) Feb 2, 2022 at 13:54

1 Answer 1

2

You can use \tl_set_rescan:Nno instead, together with category code tables:

\ExplSyntaxOn

\cctab_new:N \g_xitoxii_cctab

\cctab_gset:Nn \g_xitoxii_cctab
 {
  \cctab_select:N \c_document_cctab
  \int_step_inline:nn { 255 }
   {
    \int_compare:nT { \char_value_catcode:n { #1 } = 11 } { \char_set_catcode_other:n { #1 } }
   }
 }

\tl_set:Nn \l_tmpa_tl {abcde@f&^}
\tl_set_rescan:Nno \l_tmpa_tl { \cctab_select:N \g_xitoxii_cctab } { \l_tmpa_tl }

\tl_analysis_show:N \l_tmpa_tl

\stop

The console would show

The token list \l_tmpa_tl contains the tokens:
>  a (the character a)
>  b (the character b)
>  c (the character c)
>  d (the character d)
>  e (the character e)
>  @ (the character @)
>  f (the character f)
>  & (alignment tab character &)
>  ^ (superscript character ^).
2
  • 1
    Needless to say, this may also change the catcode of other characters, if they don't have standard catcode.
    – user202729
    Feb 2, 2022 at 9:52
  • @user202729 One has to start from some axiom…
    – egreg
    Feb 2, 2022 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .