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

Viewed 85

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

3 Answers

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.

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; }

Related