実際、コードにもっと注意を払うと、簡単に実行できます...
<cfset robots = robots.replaceAll( "(?m)^Disallow: #ReEscape(sURL)#(?:\r?\n|\z)" , "" ) />
...それらの List 関数の代わりに。
これにより、削除したばかりの行の改行が削除されますが、ファイル内の他の場所に存在する改行は削除されません (セクションを分割して読みやすくするため)。
もちろん、ファイルの最後に空白がないようにしたい場合は、trim を使用することもできます。
説明として、上記の正規表現を拡張/コメント形式で再度示します。
(?x) ## enable extended/comment mode
## (literal whitespace is ignored, hashes start comments, also ignored)
(?m) ## enable multiline mode
## (meaning ^ and $ match start/end of each line, as well as of entire input)
^Disallow:\ ## Match literal text "Disallow: " at start of a line.
## (In comment mode, a \ is needed before the space
## in standard use this is not required.)
#ReEscape(sURL)# ## use ReEscape to avoid issues since the URL might
## contain characters that are non-literal in a regex.
(?: ## non-capturing group to contain alternation between...
\r?\n ## match optional carriage return followed by a newline.
| ## or
\z ## match end of input (whether there is a newline there or not)
)
(これを CFML で使用するには、それを cfsavecontent と cfoutput の両方でラップし、結果の変数を 内に置きますrobot.replaceAll(here,'')
。)
ファイルに複数の改行がないことを本当に確認したい場合 (禁止行の削除に関連する変更に関係なく)、最も簡単な方法は次のとおりです。
<cfset robots = robots.trim().replaceAll('\r','').replaceAll('\n{2,}','\n') />
両端をトリムし、すべての改行を削除してから、少なくとも 2 つの改行のすべてのインスタンスを 1 つの改行に置き換えます。
(しかし、一般的には、複数の改行を一括で削除するよりも、最初のより具体的な表現をお勧めします。)