Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extract duplicate lines in regex

How to extract duplicate lines in regex

For Example,

KB4494507
16.0.11337.12110
KB4494507
16.0.11337.12110
KB4498509

Result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading