2017-01-13 7 views
0

私はこの質問をここで見たことがありますが、自分の要件に一致するものは見つけられません。REGEX経由でセル内容にHTMLを追加

コンテンツをWebサイトにインポートしていて、テキストの空白行にHTML(<br>)改行を追加する必要があります。私は、CalcからSublimeへの列内容をコピペしました。

私はこれをとっておきたいと思います(1つの空白行は<br>に置き換え、2つの空白行は次のセルを表します)。

 some text here some text here some text here some text here 

     some text here some text here 

     some text here some text here some text here some text here 


     some text here some text here some text here some text here 

     some text here some text here 

     some text here some text here some text here some text here 


     some text here some text here some text here some text here 

     some text here some text here 

     some text here some text here some text here some text here 

最後に終了します。

 some text here some text here some text here some text here 
     <br> 
     some text here some text here 
     <br> 
     some text here some text here some text here some text here 


     some text here some text here some text here some text here 
     <br> 
     some text here some text here 
     <br> 
     some text here some text here some text here some text here 


     some text here some text here some text here some text here 
     <br> 
     some text here some text here 
     <br> 
     some text here some text here some text here some text here 

私は正規表現での経験がなかったてきたように、私はこれで私のゾーンのうち、実際にa{1}\rのような正規表現を試してみましたが、イム。

+0

だから、あなたは、SublimeText3をあなたがされていない使用していますか?まだ何か試しましたか? –

+0

まあ、私はネット上でいくつかの正規表現の例を試しましたが、私は正規表現でほとんど何もexpしていません。 –

+0

少なくとも1つの最良の試みを共有できますか? –

答えて

0

REGEXP:

(?<=.)(?:(\r?\n){2})(?=.) 

PHP

<?php 
$re = '/(?<=.)(?:(\r?\n){2})(?=.)/m'; 
$str = 'some text here some text here some text here some text here 

some text here some text here 

some text here some text here some text here some text here 


some text here some text here some text here some text here 

some text here some text here 

some text here some text here some text here some text here 


some text here some text here some text here some text here 

some text here some text here 

some text here some text here some text here some text here'; 
$subst = '$1<br>$1'; 

$result = preg_replace($re, $subst, $str); 

echo "The result of the substitution is ".$result; 
?> 

オリジナルテキスト

ここにいくつかのテキストここにテキストここにテキストここにいくつかのテキスト

ここにいくつかのテキストここ

いくつかのテキストここにテキストここにテキストここにテキストここに

いくつかのテキストここにテキストここにテキストここにテキストここに

いくつかのテキストここにテキストここにいくつかのテキスト

ここにいくつかのテキストここにテキストここにテキストここに

いくつかのテキストここにテキストここにテキストここにテキストここに

いくつかのテキスト

ここにいくつかのテキストここ

いくつかのテキストここにテキストここにいくつかのテキストにテキストここに、ここでいくつかのテキスト

RESULT

some text here some text here some text here some text here 
<br> 
some text here some text here 
<br> 
some text here some text here some text here some text here 


some text here some text here some text here some text here 
<br> 
some text here some text here 
<br> 
some text here some text here some text here some text here 


some text here some text here some text here some text here 
<br> 
some text here some text here 
<br> 
some text here some text here some text here some text here 
1

次の正規表現を使用可能性があります

(.)((\r?\n){2})(.) 

をして$1$3<br>$3$4と交換してください。

詳細

  • (.) - 改行
  • ((\r?\n){2})以外の任意の文字にマッチキャプチャグループ1 - オプション(1又は0の出現の丁度2つの存在({2})を取り込むキャプチャグループ2 、?)キャリッジリターンと改行(単一のオカレンスはキャプチャグループ3にキャプチャされます)
  • (.) - グループ4をラインbre以外の任意の文字に一致させるak。

置換パターンには、上記のキャプチャの後方参照とリテラル<br>部分文字列が含まれています。

\r?\n\Rに置き換えて、エンジンをどのタイプの改行にも一致させることができます。