How to extract duplicate lines in regex
For Example,
KB4494507
16.0.11337.12110
KB4494507
16.0.11337.12110
KB4498509
Result:
KB4494507
16.0.11337.12110
KB4498509
tried but no luck
(.+)(\n\1)+
(.*)(\r?\n\1)+
(.*)[\r\n]*(\r?\n\1)+
(.*)(?:(?:\r?\n|\r)\1)+
>Solution :
You could use
(.*\n.*\n)\1
See a demo on regex101.com. In Javascript, this could be:
const regex = /(.*\n.*\n)\1/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(.*\\n.*\\n)\\1', 'gm')
const str = `KB4494507
16.0.11337.12110
KB4494507
16.0.11337.12110
KB4498509
`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);