2017-10-03 18 views
1

私は以下の文章を持っています。複数の行に一致する正規表現

^0001 HeadOne 


@@ 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book. 

^0002 HeadTwo 


@@ 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book. 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book. 


^004 HeadFour 


@@ 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 

^0004 HeadFour 


@@ 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book. 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been theindustry's standard dummy text ever since the 1500s, when an unknown printer took a galley of typeand scrambled it to make a type specimen book. 

以下は、私が検索に使用している正規表現です。

@@([\n\r\s]*)(.*)([\n\r\s]+)\^ 

が、これは、これらのようなだけ^0001^0003をキャッチされては、唯一の段落を持っていますが、私のテキストにマルチパラコンテンツがあります。

私はVSコードを使用していますが、誰かに私がVSコードまたはNPPでREGEXを使用してそのような複数のパラ文字列をキャプチャする方法を教えてください。

おかげ

+0

\ sは通常空白ですが、削除するか、0を示す*を付けてみてください –

+0

予想される一致は何ですか?その理由は何ですか? @カルビン、VSCodeで、 '\ s'は改行にマッチしません。末尾の境界をどのように定義できますか? '@@'は常に行頭にありますか? –

+0

@WiktorStribiżew '@@'を使用すると直接一致します – user3872094

答えて

1

VSCode正規表現に関する1つの奇妙なことは、すべての改行文字に一致しません。それらのすべてに一致させるには、[\s\r]を使用する必要があります。

@@で始まるすべての部分文字列を一致させてから、行末または文字列の最後に^まで伸ばします。

私はお勧め:

@@.*(?:[\n\r]+(?!\s*\^).*)* 

regex demo

を参照してください。注:のみに^@@.*(?:[\s\r]+(?!\s*\^).*)*、パターンの開始時に^を追加し、ラインの開始時に@@に一致します。

詳細

  • ^ - ラインの開始
  • @@ - リテラル@@
  • .* - 行の残りの部分(0+改行文字以外の文字)
  • (?:[\n\r]?(?!\s*\^).*)* - 0以上の連続した出現:
    • [\n\r]+(?!\s*\^) - 一つ以上の行が空白、その後^ CHAR
    • .* 0+と続かない休憩 - メモ帳++で

行の残りの部分、\Rが一致したところ^@@.*(?:\R(?!\h*\^).*)*を使用\h*は0個以上の水平空白に一致します(境界線では常に^が最初の文字である場合は削除されます)。

+0

NPPでそれを行い、期待通りに動作しています – user3872094

+1

@ user3872094:メモ帳++では '^ @@。*(?:\ R)?*)*'を使用します。 –

+0

いくつかの組み合わせを試してみました。 – user3872094

0

は私が/ tmp/testディレクトリにあなたの入力データを差し込まれ、これがparagrapheは$ 1内へ^で始まらない配置する必要があります

grep -Pzo "@@(?:\s*\n)+((?:.*\s*\n)+)(?:\^.*)*\n+" /tmp/test 

perlの構文を使用して動作するように、次のです。これに完全に一致するように\ rを追加する必要があるかもしれません。

+0

あなたのコードの引用符の中のビットだけを使用してください。 –

関連する問題