-3

I have a document where the matching line is []

For above:

Whenever the matching line is found, the line above that should be deleted. In this case ## mentions should be deleted.

## mentions
[]

For below:

Whenever the matching line is found, the line below that should be deleted. In this case ## mentions should be deleted.

[]
## mentions

How to achieve this using regex?

Edit: I am using VSCode regex.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
dsf
  • 1,203
  • 1
  • 6
  • 11

1 Answers1

1

Replace .*\n(?=\[\]) by nothing for above. See this demo

Replace (?<=(\[\]))\n.* by nothing for below. See this demo

Explanation for above

.*       Match all characters appearing 0 to N times
\n       Match a newline character
(?=\[\]) Make sure that [] is found just after the newline character

Explanation for below

(?<=(\[\])) Make sure our target is on the line before
\n          Match a newline character
.*          Match all characters appearing 0 to N times

Matching [] as group

To match [] as a group, simply enclose it with paranthesis like hereunder

.*\n(?=(\[\]))
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89