How to automatically join lines before certain text in notepad++

I have text file with bunch of multiple choices questions.

The questions format needs to be single lines

like this

Question
A.
B.
C.

However i found some questions containing multiple lines

like this

Question
question second line.
A.
B.
C.

Is it possible to auto join all the lines of the questions?

to be

Question question second line.
A.
B.
C.

Not all questions having multiple lines, however all questions are followed by the same numbering which is

A.
B.
C.

Thanks in advance

3 Answers

  • Ctrl+H
  • Find what: ^(?![A-Z]\.).+\K\R(?![A-Z]\.)
  • Replace with: 1 blank space
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

^ # beginning of line
(?![A-Z]\.) # negative lookahead, make sure the line doesn't begin with a capital and a dot
.+ # 1 or more any character but newline
\K # forget all we have seen until this position
\R # any kind of linebreak (i.e. \r, \n, \r\n)
(?![A-Z]\.) # negative lookahead, make sure the following line doesn't begin with a capital and a dot

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Do this:

Find what: ^([^\r\n]+)\r\n([^\r\n]+)\r\nA.
Replace with: \1 \2\r\nA.

Where [^\r\n] is any character which is not an end of line.

Place a check-mark next to "Regular expression" and ". matches newline".

enter image description here

Assuming that Question and A, B, C bullets are case-sensitive, then you can use a negative-lookahead group regular expression search and replace.

For example:

  1. Bring-up Notepad++ Search and Replace dialog by pressing [Ctrl]+H, or Search menu > Replace...
  2. Find what: \r\n(?!Question|A\.|B\.|C\.)
  3. Replace with: <-- space
  4. Search option: Match case + Wrap around
  5. Search mode: Regular expression

Essentially, the regular expression above indicates replace any CR/LF that does not appear before Question, A., B., or C. with a space character -- effectively moving incorrectly word-wrapped lines up to the Question line.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like