All is in the title. I wonder to know the difference between the two expressions \cB. and \CB\{ in regular expression (regex).

In the example below, it seems that both give the same result. Furthermore, one can mix them i.e. one can use \cB\{ ... \cE.

\tl_new:N  \l_my_tl
\NewDocumentCommand{\emphasizing}{ mm }
  \tl_set:Nn \l_my_tl { #2 }
  \regex_replace_all:nnN { (\s) (#1) (\s) } { \1 \c{emph}\cB\{ \2 \cE\} \3 } \l_my_tl
  \tl_use:N \l_my_tl
What is the difference \emphasizing{my}{in my question} what is the difference 
  • 1
    Not much most of the time. You usually don't care what character has catcode 1 or 2 because they are never typeset, they only tell TeX how to do its thing (regardless of the character code). For example the \end{verbatim} command in standard LaTeX actually uses |end[verbatim], but that makes no difference for the user. You will see the difference if, for example, you \detokenize everything: \regex_replace_all:nnN { (\s) (#1) (\s) } { \1 \c{detokenize}\cB\{ \c{emph}\cB. \2 \cE, \cE\} \3 } \l_my_tl May 12 '21 at 18:23
  • thanks for your answer. May 12 '21 at 18:28

Not a big difference in search expressions, a big one, but mostly uninfluential, in replace expressions.

In a search expression, . stands for “any character” (any token, for TeX), whereas in replace expressions . stands for the character itself. Regexes for TeX have been extended to allow for “prefixes” that state the category code the character is meant to have. Thus \cP? would not match a standard ?, which has category code 12, usually, whereas \cP means “category code 6” (mnemonic for “parameter”). For the rest, the syntax is essentially the same as in POSIX regexes.

If you add \tl_analysis_show:N \l_my_tl to the code, replacing \cE. for \cE\{, you get on the terminal

The token list \l_my_tl contains the tokens:
>  i (the letter i)
>  n (the letter n)
>    (blank space  )
>  \emph (control sequence=macro:->\protect \emph  )
>  { (begin-group character {)
>  m (the letter m)
>  y (the letter y)
>  . (end-group character .)
>    (blank space  )
>  q (the letter q)
>  u (the letter u)
>  e (the letter e)
>  s (the letter s)
>  t (the letter t)
>  i (the letter i)
>  o (the letter o)
>  n (the letter n).

The important part is

>  . (end-group character .)

which tells you're inserting . with category code 2, not }. This doesn't cause harm, because category code 2 characters all work the same. Not completely the same for category code 1 characters, because they can be used as delimiters for delimited argument when the special syntax \def\macro<tokens>#{...} is used.

In a search expression, \cB. will match any category code 1 character, whereas \cB\{ would only match {1. Not a big deal, because in normal settings there is only one such character, namely the open brace and similarly for category code 2. But there are very special situations where the state of affairs is changed (think to fancyvrb's option commandchars).

Better stick to the standard and use \cB\{ and \cE\} in replace expressions, unless you're dealing with a special situation in which you want to change the category code of { and }, but you still have something that works as group delimiter for arguments.

Note that using \cB. and \cE. would still work, because once a character gets a category code attached to it, this is permanent. However, this would be utterly confusing when examining the resulting token list.

  • OK. this is great. But, however, i have a new question now. What about \cB{? or just \{? May 12 '21 at 22:20
  • @studentmaths \cB{ cannot be generally used because of how TeX reads its input. But \cB{.*} would match {abc}. With \{ in a search expression, you match any character with character code 123 (independent of the category code). If you use \{ without prefix in a replace expression you get { with catcode 12.
    – egreg
    May 12 '21 at 22:25
  • Ok. Thank you. But with \{ without prefix in a replace expression i still get { (begin-group character {) ? May 12 '21 at 22:39
  • Thnak you again. Conclusion: i will use \cB\{ unless i have a very good reason! May 12 '21 at 22:51
  • 1
    @studentmaths No bug, actually! The syntax has changed in the very last release. But using prefixes is safer anyway.
    – egreg
    May 12 '21 at 22:54

Not much difference most of the time. As a user you usually don't care what character has catcode 1 or 2 because they are never typeset (well, you care because you have to write them, but not much more than that). Characters with catcode 1 and 2 only used by TeX to form groups, delimit arguments, and this type of thing, but the characters themselves most of the time just disappear, so the actual character doesn't matter, for example:

\catcode`\(=1 \catcode`\)=2
\tl_new:N  \l_my_tl
\NewDocumentCommand(\emphasizing)( mm )
  \tl_set:Nn \l_my_tl ( #2 )
  \regex_replace_all:nnN ( (\s) (#1) (\s) ) ( \1 \c{emph}\cB\{ \2 \cE\} \3 ) \l_my_tl
  \tl_use:N \l_my_tl
What is the difference \emphasizing(my)(in my question) what is the difference

(note that you can even intermix them, as I did in \begin{document) and \end(document} above.)

You will note the difference (I may have missed some cases, but these are the main ones):

  • when you \detokenize the characters: then the begin- and end-group tokens become normal character tokens to be typeset, then you will see the difference (same thing if you use \string or \meaning);

  • when you use a {-delimited command: for example \textcolor is defined as \protected\def\textcolor#1#{\@textcolor{#1}} (note the #{ in the definition). In this case the command only works if delimited by the same catcode-1 character:

    \catcode`\(=1 \catcode`\)=2
      \textcolor{red}(hello) % works
    % \textcolor(red)(hello) % doesn't work
  • when you are debugging code with \show or \tracingall: your eyes are so used to { and } meaning a begin- and end-group token, that \emph.word} will just look completely wrong (even though TeX will understand it).

  • thank you for the answer. May 12 '21 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.