-2

I am very new to the world of regular expressions. I am trying to use Notepad++ using Regex for the following:

Input file is something like this and there are multiple such files: Code:

abc
17
015
0 7
4.3
5/1
***END***
abc
6
71
8/3
9 0
***END***
abc
10.1
11
9
***END***

I need to be able to edit the text in all of these files so that all the files look like this:

Code:
abc
1,2,3,4,5
***END***
abc
6,7,8,9
***END***
abc
10,11,12
***END***

Also:

  1. In some files the number of * around the word END varies, is there a way to generalize the number of * so I don't have to worry about it?
  2. There is some additional data before abcs which does not need to be transposed, how do I keep that data as it is along with transposing the data between abc and ***END***.

Kindly help me. Your help is much appreciated!

  • If those 1 2 3 4 actually are numbers, and there's no other digits in the file, you can search for `(\d)\R(\d)` and replace it by `\1,\2` That would match the last digit of a line, the linefeed that follows and the first digit of the next line, and replace that by the two digits now separated by a comma. – Aaron Oct 11 '19 at 14:37
  • Thanks Aaron! Really appreciate the prompt response. – cbr600rr honda Oct 11 '19 at 15:09
  • You're welcome ! Consider accepting one of the answer even if you used the solution of my comment, it might help someone in the future and it's always nice to reward their effort :) Tim's answer is very close to my solution, and Toto's answer which does not rely on the assumption that "1 2 3 4" are numbers would be more flexible – Aaron Oct 11 '19 at 15:14
  • I am sorry Aaron, I am really new to stackoverflow. Appreciate your effort +1. Please let me know if there is any other way to reward efforts. – cbr600rr honda Oct 11 '19 at 19:23
  • Nothing to be sorry about, if someone's breaking rules here it's me (I shouldn't answer in the comments). I could still add an answer you could reward with an upvote/accepted answer but I don't feel like it's worth doing with Tim's answer being so close to mine. Your appreciation is reward enough ! – Aaron Oct 12 '19 at 01:13

2 Answers2

1

Try the following find and replace, in regex mode:

Find:    ^(\d+)\R(?!\*{1,}END\*{1,})
Replace: $1,

Demo

Here is an explanation of the regex pattern:

^                        from the start of the line
    (\d+)                match AND capture a number
    \R                   followed by a platform independent newline, which
    (?!\*{1,}END\*{1,})  is NOT followed by ***END***

Note carefully the negative lookahead at the end of the pattern, which makes sure that we don't do the replacement on the final number in each section. Without this, the last number would bring the END marker onto the same line.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

This will eplace only between "abc" and "***END***" with any number of asterisk.

  • Ctrl+H
  • Find what: (?:(?<=^abc)\R|\G(?!^)).+\K\R(?!\*+END\*+)
  • Replace with: ,
  • CHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline*
  • Replace all

Explanation:

(?:                 # non capture group
    (?<=^abc)       # positive look behind, make sure we have "abc" at the  beginning of line before  
    \R              # any kind of linebreak
  |                 # OR
    \G              # restart from last match position
    (?!^)           # negative look ahead, make sure we are not at the beginning of line
)                   # end group
.+                  # 1 or more any character but newline
\K                  # forget all we have seen until this position
\R                  # any kind of linebreak
(?!\*+END\*+)       # negative lookahead, make sure we haven't ***END*** after

Screen capture (before):

enter image description here

Screen capture (after):

enter image description here

Toto
  • 89,455
  • 62
  • 89
  • 125
  • I appreciate your support +1. – Tim Biegeleisen Oct 11 '19 at 15:12
  • Thanks Toto! Really appreciate the prompt response. Your suggestion definitely helped but actual data in my file is somewhat different and when I applied your regex it didn't work. Instead of single/double digit numbers, it can be any combination of . , /, numbers (1,2,3....) on each row. Sorry for not being clear initially. Could you please suggest the changes for it to work. – cbr600rr honda Oct 11 '19 at 15:16
  • @TimBiegeleisen: Thank you. – Toto Oct 11 '19 at 15:17
  • @cbr600rrhonda: Please, [edit your question](https://stackoverflow.com/q/58343461/372239) and add real test cases. – Toto Oct 11 '19 at 15:18
  • @cbr600rrhonda: Just replace `\d+` with `.+`, see my edit. – Toto Oct 11 '19 at 15:31
  • @Toto: Thank you so very much! This one worked to a large extent, still some hiccups. I will get back to you with the details. +1 – cbr600rr honda Oct 11 '19 at 15:39
  • @Toto: There is only one issue now: The data before the first "abc" also gets transposed along with the numbers. I only need the data between "abc" and "/***END***/" transposed. Could you please suggest the change I need to make. Your help is much appreciated on this +1. – cbr600rr honda Oct 11 '19 at 19:33
  • @cbr600rrhonda: Sorry, I've made a typo, see my edit. – Toto Oct 12 '19 at 12:20
  • @Toto It works but after few iterations of clicking Replace. Am I doing something wrong? – cbr600rr honda Oct 13 '19 at 15:48
  • @cbr600rrhonda: Probably ;) It works fine for me as you can see on capture screens for example you gave. If your text is different, may be this regex doesn't work. If your **real** file is different, please update your question with it (or significant part of it) and expected result. – Toto Oct 13 '19 at 17:08
  • @Toto: Ok, thanks for all your help, really appreciate it! +1 – cbr600rr honda Oct 14 '19 at 16:05
  • @cbr600rrhonda: You're welcome, glad it helps. If it works for you, feel free to mark the answer as accepted, [How to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Toto Oct 14 '19 at 16:16