gpt4 book ai didi

python - 如何在正则表达式中捕获从空格 +{n} 到下一个 {n}

转载 作者:行者123 更新时间:2023-12-01 03:25:02 25 4
gpt4 key购买 nike

我已经清理了一份文档,以便我能够正确地逐节撕开它。由于正则表达式较弱,我似乎无法找到正确的表达式来提取这些经文。

这是我正在使用的表达方式:

(\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]

最佳答案

只需使用 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 有四个元素,对应于上面的 {1}{4}

关于python - 如何在正则表达式中捕获从空格 +{n} 到下一个 {n},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41455359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com