Perl正規表現を使用して行を太字にする

Perl正規表現を使用して行を太字にする

私は持っています

1. Lorem
He he he

% not sure spelling
2. Lorem 
ipsun

ありますように

\textbf{1. Lorem}
He he he

% not sure spelling
\textbf{2. Lorem}
ipsun

私の擬似コードを試す

perl -000pe 's/\n\n\d./; s/\n\d.\n\\textbf\{ /g; s/$/\}/'

これは正規表現に関して以前に持っていた2つの質問に基づいています。私は数字で始まるものを一致させようとしています。試合開始と試合終了を交換します。

コードが私に与えた

Backslash found where operator expected at -e line 1, near "s/\n\n\d./; s/\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "textbf\"
Backslash found where operator expected at -e line 1, near "$/\"
    (Missing operator before \?)
syntax error at -e line 1, near "s/\n\n\d./; s/\"
Execution of -e aborted due to compilation errors.

与えられたテキストを太字にする方法は?

ベストアンサー1

-000各「段落」を「行」にするので、古典的な正規表現アンカー(および^$を使用して各「行」の先頭と終了を一致させることができます。したがって、あなたの場合、必要なものは次のとおりです。

$ perl -000pe 's/^(.+)\n/\\textbf{$1}\n/;' file 
\textbf{1. Lorem}
He he he

\textbf{2. Lorem }
ipsun

ちなみに、\エスケープ(\\\は他の文字をエスケープするために使用される特殊文字なので、独自のエスケープにも使用する必要があります。


コメントを段落の最初の行として指定できる場合、このアプローチは失敗します。数字で始まるすべての行を太字で表示する必要があります。

perl -pe 's/^\d\..+/\\textbf{$&}/' file

おすすめ記事