nixnotwin
nixnotwin

Reputation: 2403

Find, cut and paste regex in sublimeText3

I have the following in my text file:

53\footnote{Author Name, \textit{Book Title}, p..}
3\footnote{Author Another, \textit{Book Old}, p..}
5-6\footnote{Author Onemore, \textit{Book New}, p..}

I have a few hundred of these types of text. I have arrived here after using simple regex. It's Latex markup text. These are not in separate lines, they are in between the text. With regex, I want to move the digits at the beginning and placed like this: p.53. It should look like so:

\footnote{Author Name, \textit{Book Title}, p.53.}
\footnote{Author Another, \textit{Book Old}, p.3.}
\footnote{Author Onemore, \textit{Book New}, p.5-6.}

If not in Sublime, I can use any tool available on Linux.

Edit 1: There are a few numbers like years and dates in my text. So, the solution must avoid changing them. Also, the size of the footnotes varies in some cases. But p..} remains the same throughout.

Upvotes: 1

Views: 107

Answers (1)

MattDMo
MattDMo

Reputation: 102922

In Sublime, select Find → Replace….

In the Find: field, enter

([\d-]+)(\\footnote.*?\}, p\.)

and in the Replace: field, put

\2\1

screenshot of Sublime Text

Explanation:

There are two capturing groups, enclosed in parentheses (). The first one captures one or more characters consisting of digits 0-9 (\d) and the hyphen character (-). This is the page number or page range, assuming there are no spaces between characters.

The second capture groups starts capturing at the \footnote LaTeX directive and selects all characters up to and including }, p.. We are now ready to replace, which is a very simple operation - just reverse the position of the two captured groups, signified by \2\1 in the replace field.

See the demo at regex101.com here.

Again, this is assuming that all of your targets are formatted exactly as you've shown in the question, with no extraneous characters or spacing.

Upvotes: 2

Related Questions