Home > Software engineering >  How to find same variable with different value using RegEx?
How to find same variable with different value using RegEx?

Time:11-29

Here is the example.

0x13F4E010 = 4 new items
0x13F4E010 = 5 new items of clothing available

I only want to find the same variable with different value with RegEx, so i can edit it manually.

So, basically i only need help to find these duplicated variable behind different value.

0x13F4E010 = 
0x13F4E010 = 

Sorry for bad english. Thank you in advance.

I find this example in internet

^(.*)(\W\1) $

but that only find duplicate with same variable and same value. Like this...

    0x14C79222 = Camo Sweatpants
    0x14C79222 = Camo Sweatpants

CodePudding user response:

A sample regex that can do the job is

^(\S ) = (. )\r?\n\1 = (. )$

See the regex demo.

Details:

  • ^ - start of a line
  • (\S ) - Group 1: one or more non-whitespace chars
  • = - a = with one space on both sides
  • (. ) - Group 2: one one or more chars other than line break chars as many as possible
  • \r?\n - CRLF or LF ending
  • \1 - same value as in Group 1
  • = - a = with a space on each side
  • (. ) - Group 3: one one or more chars other than line break chars as many as possible
  • $ - end of a line.

Note you might need an (?m) inline regex option (or code equivalent) to make ^ and $ match start/end of each line instead of the whole string (depends on the environment).

  • Related