-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
  • 81,480
  • 12
  • 93
  • 144
dsf
  • 83
  • 6

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
  • 20,020
  • 9
  • 41
  • 81