In a project I am working on, I use subfiles to compile a document, each containing one section. If I compile only the document containing some given section, I want the page number to be correct. One way to solve this would be to set the page number manually before I compile, but I would prefer if this worked automatically. The main file, which compiles the complete document, contains a table of contents, why there is a .toc file which contains this information. Given that this does not seem to be a built in functionality in the subfiles package, and given that I do not want to use another package than subfiles to divide my document into several files, I would therefor want to write a macro which given a section number returns the first page of that section as recorded by the .toc file of the main document.
One way to do this should be to, after reading the contents of this file in some way, use a regular expression to find the pattern {number}{section.X} in the toc file, given a section number X. However, when trying to do this with the l3regex package, I got stuck when trying to understand how to use \regex_extract_once to extract the page number for the a section. The code below uses the regular expression { (\d{1}{2}) .. section.X} to find the page number given that X is the section number, but simply replacing replace with extract does not work.
\documentclass{article}
\usepackage{l3regex}
\ExplSyntaxOn
\tl_new:N \l_demo_tl
\seq_new:N \l_demo_res
\cs_new:Npn \demo #1 {
\tl_set:Nn \l_demo_tl {#1}
\regex_replace_once:nnN { (\d{1,2}) .. section\.1} {..............\1...............} \l_demo_tl
\l_demo_tl
}
\ExplSyntaxOff
\begin{document}
\demo{contentsline {section}{numberline {1}Aritmetik och algebra}{3}{section.1}}
\end{document}
So, is there some easier way to obtain this, or alternatively, how does \regex_extract_once work, i.e. how can the code above be adjusted to print the matched string?

.tocfile? Can you be more specific about how you want to use this? – egreg Dec 03 '14 at 09:56\contentsline, which can be added to the macro itself. – egreg Dec 03 '14 at 10:07\regex_extract_once:nnNhas different arguments than\regex_replace_once:nnNso it's not just a matter of replacing one function into another. That said, even using the right syntax fails here because the extracted part does not have balanced braces so it cannot be extracted (on top of this, there is currently a bug, see https://github.com/latex3/latex3/issues/377 ). Besides @egreg's solution a work-around is to turn the input to a string before applying\regex_extract_once:nnN. – Bruno Le Floch Jul 12 '17 at 16:27