1

Minimum Data to regex

\documentclass{article}
\begin{document}

-------------------------------------------------------------
A           B       C               D
Header      Aligned Aligned         Aligned
----------- ------- --------------- -------------------------
First       row     12.0            Example of a row that
                                    spans multiple lines.

Second      row     5.0             Here's another one. Note
                                    the blank line between
                                    rows.
-------------------------------------------------------------

Table: Here's the caption. It, too, may span
multiple lines.

\section{Lorem Ipsun}
Hello world!

-------------------------------------------------------------
A           B       C               D
Header      Aligned Aligned         Aligned
----------- ------- --------------- -------------------------
First       row     12.0            Example of a row that
                                    spans multiple lines.

Second      row     5.0             Here's another one. Note
                                    the blank line between
                                    rows.
-------------------------------------------------------------

Table: Here's the caption. It, too, may span
multiple lines.

\end{document}

Desired output

-------------------------------------------------------------
A           B       C               D
Header      Aligned Aligned         Aligned
----------- ------- --------------- -------------------------
First       row     12.0            Example of a row that
                                    spans multiple lines.

Second      row     5.0             Here's another one. Note
                                    the blank line between
                                    rows.
-------------------------------------------------------------

Table: Here's the caption. It, too, may span
multiple lines.

-------------------------------------------------------------
A           B       C               D
Header      Aligned Aligned         Aligned
----------- ------- --------------- -------------------------
First       row     12.0            Example of a row that
                                    spans multiple lines.

Second      row     5.0             Here's another one. Note
                                    the blank line between
                                    rows.
-------------------------------------------------------------

Table: Here's the caption. It, too, may span
multiple lines.

I want extract all tables from LaTeX document.

Pseudocode

  1. match >7 of "-" in a row and until everything until "Table:". Include the line with "Table:" but not not anything after that line.
  2. iterate 1) until the end of the file

My attempt

The first step

[-]{10,777}$

and to include now everything except word "Table:"

((?!Table:).)*$

and include finally everything from line with "Table:"

^(?=.*?\Table:\b)

All combined

[-]{10,777}$((?!Table:).)*$^(?=.*?\Table:\b)

which cannot work. There is something wrong but I do not know what.

How can you regex such an environment well in Perl?

  • Use pandoc to parse the LaTeX file, then select the interesting tables in the result, then use pandoc again to convert the result back to LaTeX. – lcd047 May 18 '15 at 17:15
  • 1) This is not a LaTeX file 2) please edit and show us your desired output. Why do you both not want and want the caption? – terdon May 18 '15 at 17:23
  • @Icd047 I want to extract all tables. I am not aware that you can as efficient parsin directly with Pandoc as you can with terdon's regex. – Léo Léopold Hertz 준영 May 18 '15 at 18:50
  • @terdon This is a pre-form of the tex file. Here, I am thinking the automation of different elements in note taking and presentation. The final output will be eventually a LaTeX file but, at this point, everything is little simplified tex.stackexchange.com/q/245572/13173 – Léo Léopold Hertz 준영 May 18 '15 at 18:51
  • @Costas Good alternative! I think wrapping this command in a function where looping lines is an option. However, this approach may not be so extensible as the regex. – Léo Léopold Hertz 준영 May 18 '15 at 18:56
2

I'll edit this if you update the question but I think you're looking for something like this:

perl -007lne '@F=(/-{7,}.*?Table:.*?\n(?=\n)/gsm); print join "\n", @F' file.tex 

Explanation

  • -007 : slurp the entire file
  • -lne : add a new line to each print call, process the input file, and run the script given by -e.
  • @F=(/pattern/gsm) : save all matches of pattern in the array @F. The g turns on global matching, the s makes . match newlines and the m causes the match operator to match across multiple lines.
  • -{7,}.*?Table:.*?\n(?=\n) : match 7 or more -, then anything until the 1st Table: (.*?Table:), then anything until the first two consecutive newlines (.*?\n(?=\n)). I'm using a lookahead only to avoid printing both newlines.
  • print join "\n", @F : print each element of the array @F, separating them with newlines.
  • I think you understood me correctly. I added the desired output above. Yes, I want the caption which ends when a new empty line. – Léo Léopold Hertz 준영 May 18 '15 at 18:48
  • 1
    @Masi thanks for the update. OK, then this should be pretty much what you were looking for. The important points are i) X{N,} will match X N or more times; ii) you need the m switch if you want the match operator to match across multiple lines and the s switch if you want . to match a newline; iii) you don't need to use $, .* will match until the end of the line unless you tell it not to; iv) $() makes no sense in a regex. – terdon May 18 '15 at 18:57
2
sed -n '/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc

If you wants newline after each table:

sed -n '/^\s*Table:/G;/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc

or

sed '/-\{10,777\}/,/^\s*Table:/! d;/^\s*Table:/G' LaTeX.doc

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.