擅长:python、mysql、java
<p>Perl代码算法</p>
<ul>
<li>用正则表达式将文本与脚注分开</li>
<li>替换每个脚注</li>
</ul>
<pre><code>use strict;
use warnings;
use feature 'say';
my $text;
my %footnote;
/^\[(\d+)\] (.*)\Z/ ? $footnote{$1} = $2 : ($text .= $_) while <DATA>;
$text =~ s/\$\^\{\[$_\]\}\$/\\footnote{$footnote{$_}}/g for keys %footnote;
say $text;
__DATA__
[text]
This is an example.$^{[1]}$ I like it.$^{[2]}$
[text]
[1] I'm a footnote!
[2] I'm also a footnote!
</code></pre>
<p>输出</p>
<pre><code>[text]
This is an example.\footnote{I'm a footnote!} I like it.\footnote{I'm also a footnote!}
[text]
</code></pre>