0

I literally spent hours yesterday trying to replace line breaks in notepad++.

I had something like:

"1:30 And to every beast of the earth, and to every fowl of the air,
and to every thing that creepeth upon the earth, wherein there is
life, I have given every green herb for meat: and it was so. 

1:31 And God saw every thing that he had made, and, behold, it was
very good. And the evening and the morning were the sixth day. 

2:1 Thus the heavens and the earth were finished, and all the host of
them. 

2:2 And on the seventh day God ended his work which he had made; and
he rested on the seventh day from all his work which he had made."

Surprisingly, in notepad++, Regex doesn't recognize /n/n. When I run the search, it found nothing, even though there is clearly a double line break at the end of each verse. Was very fustrated. The solution was to find \r\n\r.

But that adds a break at the end of every verse. How can I add a break at the end of every last verse in a chapter using Regex? The first number before the colon is the chapter.

I should find something like [0-9]:[0-9]+[A-Z].... right?


Edit, that is I wish to go from:

1:30 And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein there is life, I have given every green herb for meat: and it was so.

1:31 And God saw every thing that he had made, and, behold, it was very good. And the evening and the morning were the sixth day.

2:1 Thus the heavens and the earth were finished, and all the host of them.

2:2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made

to something like:

1:30 And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein there is life, I have given every green herb for meat: and it was so.

1:31 And God saw every thing that he had made, and, behold, it was very good. And the evening and the morning were the sixth day.

    <br>

2:1 Thus the heavens and the earth were finished, and all the host of them.

2:2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made

Another example is:

2:23 And Adam said, This is now bone of my bones, and flesh of my
    flesh: she shall be called Woman, because she was taken out of Man.

2:24 Therefore shall a man leave his father and his mother, and shall
    cleave unto his wife: and they shall be one flesh.

2:25 And they were both naked, the man and his wife, and were not
    ashamed.

3:1 Now the serpent was more subtil than any beast of the field which
    the LORD God had made. And he said unto the woman, Yea, hath God said,
    Ye shall not eat of every tree of the garden?  3:2 And the woman said
    unto the serpent, We may eat of the fruit of the trees of the garden:

3:3 But of the fruit of the tree which is in the midst of the garden,
    God hath said, Ye shall not eat of it, neither shall ye touch it, lest
    ye die.

to something like:

    2:23 And Adam said, This is now bone of my bones, and flesh of my
    flesh: she shall be called Woman, because she was taken out of Man.

    2:24 Therefore shall a man leave his father and his mother, and shall
    cleave unto his wife: and they shall be one flesh.

    2:25 And they were both naked, the man and his wife, and were not
    ashamed.

    <br>

    3:1 Now the serpent was more subtil than any beast of the field which
    the LORD God had made. And he said unto the woman, Yea, hath God said,
    Ye shall not eat of every tree of the garden?  3:2 And the woman said
    unto the serpent, We may eat of the fruit of the trees of the garden:

    3:3 But of the fruit of the tree which is in the midst of the garden,
    God hath said, Ye shall not eat of it, neither shall ye touch it, lest
    ye die.
Toto
  • 89,455
  • 62
  • 89
  • 125

2 Answers2

1

I made this and tested it in Notepad++. Does this fulfill your needs? If there aren't double line breaks in your input text file, swap out the \r\n\r\n in the below for just \r\n.

(\d+)(:\d+\s.+\r\n\r\n)((\1:.*\r\n\r\n)+)

Replace with:

\1\2\3\r\n

Test strings:

1:30 And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the earth, wherein there is life, I have given every green herb for meat: and it was so.

1:31 And God saw every thing that he had made, and, behold, it was very good. And the evening and the morning were the sixth day.

1:32 Lorem ipsum

2:1 Thus the heavens and the earth were finished, and all the host of them.

2:2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made.

Edit (to account for within-verse newline characters):

(\d+)(:\d+\s(.|(\r\n[^\r\n]))+\r\n\r\n(\1:(.|(\r\n[^\r\n]))*\r\n\r\n)+)

Replace with:

\1\2\r\n
ejkeep
  • 318
  • 1
  • 6
  • Thank you for trying to help. I don't think it worked. Let me try and edit my post. – Kenneth Dang Apr 21 '22 at 20:18
  • Sure, if you can paste a chunk of the text file you're working with, that would confirm it. – ejkeep Apr 21 '22 at 20:20
  • I do have a feeling it isn't possible though. There would have to be a way for ReGeX to find when the first value (before the semicolon) is different..... – Kenneth Dang Apr 21 '22 at 20:25
  • Ah, now that you edited your post, I see newline characters in your input. – ejkeep Apr 21 '22 at 20:27
  • So in your first example, my solution works (make sure to uncheck ". matches newline). In the second example, it doesn't work because there are newline characters within verses. Do you want to retain those newline characters? If not, removing them with another regex would solve it. – ejkeep Apr 21 '22 at 20:35
  • I'll edit my post so it works either way. – ejkeep Apr 21 '22 at 20:40
  • I marked it as the right answer. How are you so good with ReGeX? I didn't even think it was possible. I've only encountered it in FreeCodeCamp. What resources do you have to get better at this? – Kenneth Dang Apr 21 '22 at 20:58
0

Regex:

(?<verse>(?<block>^(?<num>\d+):\d+\s(?<txt>(?s).*?)(?<end>\n{2})(?!(?P=num)))+?)

How it works:

Named capture groups:

  • verse - target we wish to add a line-break after.
  • block - single sub element of a verse, starts at the beginning of line ^
  • num - current verse number.
  • txt - text content.
  • end - end of block, uses two consecutive line-breaks

*some named groups captured just for the ease of explaining.

Mechanism:

  • verse will contain as much blocks as possible +? until it reaches a block with different num, using negative look-ahead ?! positioned after end and should not match the last value of group num.
  • txt use local modifier s (single-line) to include line breaks when using .

Replace (verse with itself + <br>)

\n${verse}<br>\n\n
LironZ
  • 177
  • 2
  • 8