Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to write a command that searches a file for a string that matches a certain regexp, and inserts that string. I know how to parse a string of text using a regex with

\cs_new_protected:Npn \mycom #1
    \tl_set:Nn \l_tmpa_tl {#1}
    \regex_replace_all:nnN { THE-REGEXP } { THE-REPLACEMENT } \l_tmpa_tl
    \tl_use:N \l_tmpa_tl

but I don't know how I can use it to parse the contents of a file. I tried replacing {#1} with {\input{#1}}, but it didn't work (the \input was simply considered part of the string).

share|improve this question
Take a look at the catchfile package to allow you to read an entire file to a macro, or iterate over the file line-by-line with \ior_map_inline:Nn (experimental function). –  Joseph Wright Apr 25 '12 at 7:58

1 Answer 1

up vote 5 down vote accepted

There is no facility, at the moment, for storing into a token list the contents of a file, but you can still use the catchfile package:

\cs_new_protected:Npn \mycom #1
    \CatchFileDef \l_tmpa_tl {#1} {}
    \regex_replace_all:nnN { xrep } { foo } \l_tmpa_tl
    \tl_use:N \l_tmpa_tl

share|improve this answer
Thanks, that's perfect. Any chance I might get LaTeX to interpret the output? =) (instead of just displaying it verbatim) –  Malabarba Apr 25 '12 at 10:07
@BruceConnor I get what's expected. Probably what you get wrong depends on the "replacement" regex you're using. –  egreg Apr 25 '12 at 10:14
What do you mea by that? The regexp does output what I expect it to ({\bf Test} for example). The problem is that this output does't get iterpreted by LaTeX (so my pdf would contain a literal {\bf Test} istead of a Test). Is that not the expected behaviour? –  Malabarba Apr 25 '12 at 10:23
@BruceConnor If the search regex is something like ([^\*]*?)\*\* and the replacement regex is \c{textbf}\cB\{\1\cE\} I get that **Test** is replaced by \textbf{Test} and interpreted correctly. –  egreg Apr 25 '12 at 10:29
If I copy/paste your answer's preamble to a new document, replace xrep with .* and foo with \c{textbf}\cB\{ Hi \cE\}, create a file in the same folder cotaining **TEST**, and call \mycom{filename} inside begin/end{document} I get the bizarre: c–textbf ̋cB–HicE ̋c–textbf ̋cB–HicE ̋ –  Malabarba Apr 25 '12 at 11:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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