# Defining a find and replace algorithm using LaTeX3's l3regex

I've been trying to work out the mechanics of LaTeX3's regular expression system as implemented in l3regex, but am having some difficulty understanding how/why it is acting as it is.

If I use as an example the following code:

\documentclass{article}
\usepackage{expl3}
\usepackage{l3regex}
\ExplSyntaxOn
\cs_new:Npn \demo #1 {
\tl_new:N \l_demo
\tl_set:Nn \l_demo {#1}
\regex_replace_all:nnN {\_*?\_} {\emph{\1}} \l_demo
\tl_use:N \l_demo
}
\ExplSyntaxOff
\begin{document}

\demo{This is a _test_ document.}

\end{document}


The following text will be printed on the page:

This is a œmph– ̋testœmph– ̋ document.

But I would have expected to see the following:

This is a test document.

Similar results arise through the use of other regular expressions similar to the above pattern.

Would anyone be able to explain what is happening in this example, and how problems such as this might be fixed?

-
The pattern \_*?\_ looks (lazily) for any sequence of underscores followed by an underscore. Thus the first underscore matches this patterns and it's changed into the string \emph{} where \e represents the escape character (code 0x1B, which is ignored in print) and the braces are ordinary characters. –  egreg Dec 28 '12 at 14:31

The following produces the result you are looking for:

\documentclass{article}
\usepackage{expl3}
\usepackage{l3regex}
\ExplSyntaxOn
\tl_new:N \l_demo_tl
\cs_new:Npn \demo #1 {
\tl_set:Nn \l_demo_tl {#1}
\regex_replace_all:nnN { \_(.*?)\_ } { \c{emph}\cB\{ \1 \cE\} } \l_demo_tl
\tl_use:N \l_demo_tl
}
\ExplSyntaxOff
\begin{document}

\demo{This is a _test_ document.}

\end{document}


In the matching expression we have

• \_ giving the underscore character
• (...) providing a group of characters that are to be remembered and used as \1 in the replacement text
• .*? which matches (lazily) at zero or more occurrances of any character

In the replacement text

• \c{...} provides a control sequence, so
• \c{emph} is your emphasise command
• \cB\{ produces the { character with the class of an opening token, for the beginning of the argument to your emphasise command
• \cE\} ends the argument group.

In regular expressions and their replacements \ introduces a number of special constructions with non-standard meanings. The documentation gives some good examples.

As egreg points out, escaping _ is not strictly necessary, but the documentation recommends it, saying:

non-alphanumeric printable ascii characters can (and should) always be escaped

Note that, following egreg's kind remarks, in the code above, the variable \l_demo was renamed in the above code to \l_demo_tl in compliance with LaTeX3 syntax conventions; the _tl indicates this holds a token list. Also, the declaration of the variable is only needed once, so this is moved out of the control sequence.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6807986497879028, "perplexity": 3989.4473682462194}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-11/segments/1424936463122.1/warc/CC-MAIN-20150226074103-00318-ip-10-28-5-156.ec2.internal.warc.gz"}