2017-01-04 36 views
1

私は詩でそれを正しく拾うために文書を整理しました。正規表現が弱いので、私はこれらの句を抽出する正しい表現を見つけることができないようです。正規表現で空白+ {n}から次の{n}までをキャプチャする方法

これは、私が使用しています式です:

(\t?\t?{\d+}.*){ 

そして、私はそれは問題ではありません期待しても私は、Pythonでこれをやっています。

これを変更して単に{x} some verse {x} next verseの句をハイライト表示させるにはどうすればよいですか?

ご覧のとおり、この文書では詩スタイルの書き方に注意を払っているため、タブを意識したままにしています。ここ

とは、例えば、ドキュメントです:

{1} The words of the blessing of Enoch, wherewith he blessed the elect [[[[and]]]] righteous, who will be living in the day of tribulation, when all the wicked [[[[and godless]]]] are to be removed. {2} And he took up his parable and said--Enoch a righteous man, whose eyes were opened by God, saw the vision of the Holy One in the heavens, [[which]] the angels showed me, and from them I heard everything, and from them I understood as I saw, but not for this generation, but for a remote one which is for to come. {3} Concerning the elect I said, and took up my parable concerning them: 

     The Holy Great One will come forth from His dwelling, 
     {4} And the eternal God will tread upon the earth, (even) on Mount Sinai, 
     [[And appear from His camp]] 
     And appear in the strength of His might from the heaven of heavens. 

     {5} And all shall be smitten with fear 
     And the Watchers shall quake, 
     And great fear and trembling shall seize them unto the ends of the earth. 

     {6} And the high mountains shall be shaken, 
     And the high hills shall be made low, 
     And shall melt like wax before the flame 

     {7} And the earth shall be [[wholly]] rent in sunder, 
     And all that is upon the earth shall perish, 
     And there shall be a judgement upon all (men). 

     {8} But with the righteous He will make peace. 

     And will protect the elect, 
     And mercy shall be upon them. 

     And they shall all belong to God, 
     And they shall be prospered, 
     And they shall [[all]] be blessed. 

     [[And He will help them all]], 
     And light shall appear unto them, 
     [[And He will make peace with them]]. 

     {9} And behold! He cometh with ten thousands of [[His]] holy ones 
     To execute judgement upon all, 
     And to destroy [[all]] the ungodly: 

     And to convict all flesh 
     Of all the works [[of their ungodliness]] which they have ungodly committed, 
     And of all the hard things which ungodly sinners [[have spoken]] against Him. 

[BREAK] 
[CHAPTER 2] 

答えて

1

は単にre.splitで詩マーカー上にテキストを分割:

import re 
text = '''{1} The words of the blessing of Enoch, wherewith he blessed the elect [[[[and]]]] righteous, who will be living in the day of tribulation, when all the wicked [[[[and godless]]]] are to be removed. {2} And he took up his parable and said--Enoch a righteous man, whose eyes were opened by God, saw the vision of the Holy One in the heavens, [[which]] the angels showed me, and from them I heard everything, and from them I understood as I saw, but not for this generation, but for a remote one which is for to come. {3} Concerning the elect I said, and took up my parable concerning them: 

     The Holy Great One will come forth from His dwelling, 
     {4} And the eternal God will tread upon the earth, (even) on Mount Sinai, 
     [[And appear from His camp]] 
     And appear in the strength of His might from the heaven of heavens.''' 

result = [i for i in re.split(r'\{\d+\}', text) if i] 

resultは、上記{4}を通じて{1}に対応し、4つの要素があります。

+0

これは良い解決策かもしれません。私はそれに切り替えるかもしれない。 – Pipsqweek

0

上記の答えは良いですが、この本では節が常に正しくインクリメントされているわけではありません(つまり、原稿の詳細のために5節から7節に飛ぶことができます)それらは後になります。基本的には、全体の詩と数を抽出しなければならなかった。文脈では

verse = re.compile(r'([\t+]?{\d+}[^{]*)', re.DOTALL) 

レシピはこのように見えた

import re 

f = open('thebook.txt', 'r').read() 

chapters = f.split('[BREAK]') 

verse = re.compile(r'([\t+]?{\d+}[^{]*)', re.DOTALL) 

verses = re.findall(verse, chapters[1]) 

注意してください、正常に動作しているようですが、私は確かにそれが占め作るために、結果を確認する必要がすべて。

+0

最後に '{'がなければ、最後の詩にすべての文書を取り込む – vks

1
(\t?\t?{\d+}.*?)(?={) 

デモを参照してください。

https://regex101.com/r/OCpDb7/1

編集:

あなたは

(\t?\t?{\d+}.*?)(?={|\[BREAK\]) 

参照のデモを使用し、同様に最後の詩をキャプチャしたい場合。

https://regex101.com/r/OCpDb7/2

2問題に苦しんであなたのオリジナルの正規表現。あなたはそれが既にキャプチャされているように、その詩を一致させることができないだろう{をキャプチャしてgreedy operator.Use non greedy.*?

2)を使用していた

(\t?\t?{\d+}.*){ 
      ^^ 

1)。アサートするだけでキャプチャしない場合は、lookaheadを使用します。

+0

両方の文書の比較を行うと、生成された文書の文章は使用された文書よりも多くなります。私の場合、それは等しいです。しかし、それはまだ私が引き起こしている問題かもしれないので、私はそれに戻ってくるでしょう。 – Pipsqweek

+0

@Pipsqweek正しい表現はhttps://regex101.com/r/OCpDb7/2でなければなりません。最後の節には '{'が先行していないので、 '[BREAK]' – vks

+0

@Pipsqweekあなたは新しい質問をする前に答えの一つを受け入れるべきです.....もし何も働かなかったらあなたはpplに知らせるべきです....この質問が閉じられるように – vks

関連する問題