Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How to match all lines that have two words that are not the same?

Is it possible to use a Regex to match all lines that have 2 words that are not the same ?

I do understand that I can use \d+: ([,\w]+ )\1|(.+)/gm
This is where you capture the lines with the same word
Then capture everything else
Then you run the function to only do something with group 2
But is there a way to only match the lines that are not the same

1: 43,186 43,186
2: 296,531 296,531
3: 108086 108086
4: abcdef abcdef
5: Capture This
6: 497,802 497,802
7: 200,062 300,062
8: Not Same
9: 202232 252232
10: 4,324,452 4,324,452

Example:

let string = `
1: 43,186 43,186 
2: 296,531 296,531 
3: 108086 108086 
4: abcdef abcdef 
5: Capture This 
6: 497,802 497,802 
7: 200,062 300,062 
8: Not Same 
9: 202232 252232 
10: 4,324,452 4,324,452 `;

let magicalRegex = /\d+: (This One) (Does not match this one)/gm;

string.replace(magicalRegex, matched=>{
  /*These are the matches I found master*/
`
5: Capture This
7: 200,062 300,062
8: Not Same
9: 202232 252232
`
/*Run some special code*/
/*Return matched*/
});

First   Second      Third
isNum: Anything  Must Match Second

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use /^\d+:\s+(\S+)\s+(?!\1$)\S+$/gm regex to extract the matches and then simply join the items, see the regex demo and the JavaScript demo below:

let string = `1: 43,186 43,186
2: 296,531 296,531
3: 108086 108086
4: abcdef abcdef
5: Capture This
6: 497,802 497,802
7: 200,062 300,062
8: Not Same
9: 202232 252232
10: 4,324,452 4,324,452`;

let magicalRegex = /^\d+:\s+(\S+)\s+(?!\1$)\S+$/gm;

console.log( string.match(magicalRegex).join("\n") )

The ^\d+:\s+(\S+)\s+(?!\1$)\S+$ regex matches

  • ^ - start of line
  • \d+ - one or more digits
  • : - a colon
  • \s+ - one or more whitespaces
  • (\S+) - Group 1: one or more non-whitespace chars
  • \s+ - one or more whitespaces
  • (?!\1$) - a negative lookahead that fails the match if there there is a Group 1 value immediately on the right till end of string
  • \S+ - one or more non-whitespace chars
  • $ - end of line.
about 4 years ago · Juan Pablo Isaza Denunciar

0

For the OP's use case and requirements a simple regex which matches and captures a non whitespace (sequence) separated by a whitespace (sequence) additionally needs to ensure the uniqueness of each new-line content via a backreference used within a negative lookahead ... /^(\S+)\s+(?!\1).*/gm ...

console.log(
`43,186 43,186
296,531 296,531
108086 108086
abcdef abcdef
Capture This
497,802 497,802
200,062 300,062
Not Same
202232 252232
4,324,452 4,324,452`.match(/^(\S+)\s+(?!\1).*/gm)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

And in order to fully match the OP's example code one has to refine the 1st regex from /^(\S+)\s+(?!\1).*/gm to /^(?:\d+\:\s*)(\S+)\s+(?!\1).*/gm adding (?:\d+\:\s*) to the former which does match the digit-colon-whitespace pattern in the beginning of each new-line ...

console.log(
`1: 43,186 43,186 
2: 296,531 296,531 
3: 108086 108086 
4: abcdef abcdef 
5: Capture This 
6: 497,802 497,802 
7: 200,062 300,062 
8: Not Same 
9: 202232 252232 
10: 4,324,452 4,324,452`.match(/^(?:\d+\:\s*)(\S+)\s+(?!\1).*/gm).join('\n')
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

Well if I understood you correctly the regex would look something like this

string regex = @"^(\w+)\s(?:(?!\1).)*$";

Here is the fiddle: https://dotnetfiddle.net/0ueUKd And checked here: https://www.regexpal.com/

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda