Reputation: 31
I am trying to find a regex solution to keep 

and remove other things without breaking the line order. Only some of the lines contains this pattern one or more times. I tried (?<=
)(.+)|(.+)(?=
)|^((?!
).)*$
, but it only keeps one from each row, although they contain more. For example, I have something like that:
The client requires photos of a radioactive world
Reach the target planet.
The client requires photos.

Reach the target planet.
The client requires photos of a desert world
Reach the target planet.
The client requires photos of an airless world. Reach the target planet.
The client requires photos of a strange world

Reach the target planet
Make a quick scan.
Expecting exactly this:











I would be glad if you help.
Upvotes: 2
Views: 737
Reputation: 163207
You can make use of SKIP FAIL to match 

and then not consume the match.
Then match all characters except &, and when it does encounter &, assert that it is not directly followed by #xA;
Find what

(*SKIP)(*FAIL)|[^&\r\n]+(?:&(?!#xA;)[^&\r\n]*)*
Replace with:
Leave empty
Explanation
#xA;
Match literally(*SKIP)(*FAIL)|
Consume the characters that you want to avoid[^&\r\n]+
Match 1+ times any char except &
or a newline(?:
Non capture group
&(?!#xA;)
Match & if not directly followed by #xA;
[^&\r\n]*
Match 0+ times any char except & or a newline)*
Close the non capture group and repeat 0+ timesUpvotes: 1
Reputation: 3220
You could use a capturing group.
(.*?)((?:
){0,})
Details:
(.*?)
: Group1 - matches any characters as few as possible((?:
){0,})
: Group2 - matches 

or notUpvotes: 1