6

I have a file with the following lines (condensed example, real file is 1.000+ lines):

...
type1.value1=60  <-- replace 60 with 72 from line 5
type1.value2=15  <-- replace 15 with 14 from line 6
type2.value1=50  <-- replace 50 with 72 from line 5
type2.value2=18  <-- replace 18 with 14 from line 6
type3.value1=72  
type3.value2=14
...

I want to replace all values from type(x) with the values from type3. There are many type/value combinations, so i would like to avoid handwork. Also, i have to do this really often.

Is that possible with Notepad++ Regex find/replace?

The matching expression is the following, where the first group should stay the same and the second should be replaced by the result of yet anoter regex.

^type1.([\w]+)=([\S]+)
Harper
  • 1,073
  • 1
  • 10
  • 23
  • You can use FIND/REPLACE option in Notepad++ – SolveProblem Aug 30 '18 at 08:52
  • How does that automatically find the "72" in line 3? As i mentioned, i have a LOT of entries and the example is just a small snippet. There are over 200 cases and 1000+ lines in the file. – Harper Aug 30 '18 at 08:59
  • Is the line with the desired value always 2 lines later? – Sebastian Proske Aug 30 '18 at 09:03
  • @Harper your question is little bit confusing. can you provide some example? as you said you want to replace "FzgF80.RxDBGrid1.Item15.Width=60" with "FzgF84.RxDBGrid1.Item15.Width=72 " and now you want to replace 72 with??? please be more clear. – SolveProblem Aug 30 '18 at 09:09
  • @Harper DOES "FzgF80.RxDBGrid1.Item15.Width=60" always has "60" as initial value? – SolveProblem Aug 30 '18 at 09:10
  • No it does not. The values are unknown, as well as the line positions. I will simplify the example above to make it clearer. – Harper Aug 30 '18 at 09:24
  • @bobblebubble Does notepad++ support `\K` ? – Paolo Aug 30 '18 at 10:03
  • @UnbearableLightness [yes, it does](https://stackoverflow.com/a/13543042/5527985) – bobble bubble Aug 30 '18 at 10:05
  • Neat, then post your comment as an answer ;) – Paolo Aug 30 '18 at 10:06
  • Tanks for the suggestion, i definetly learned something from that. Unfortunately i dont know the distance between the entries. – Harper Aug 30 '18 at 10:07
  • 1
    If that `valueX` in type `N` should be replaced with the same `valueX` in type `3` then see the live demo here https://regex101.com/r/50QXfc/1 – revo Aug 30 '18 at 10:13
  • Awesome. Thank you. If you post it as an answer i will accept it. – Harper Aug 30 '18 at 11:16
  • OK, it seems my notepad++ does not support \K. I could modify the regex using capturing groups to the following: Regex: (FzgF(?!84\.)\d{2})\.(\S+)=\S+(?=[\s\S]*?FzgF84\.\2=(\S+)) Substitution: \1.\2=\3 – Harper Aug 30 '18 at 12:35
  • 1
    @Harper or you update your np++ – bobble bubble Aug 30 '18 at 13:13

1 Answers1

3

Regex:

type(?!3\.)\d+\.value(\d+)=\K\d+(?=[\s\S]*?type3\.value\1=(\d+))

Replace with:

\2

Explanation:

  • type(?!3\.)\d+ Match a type other than 3
  • \.value(\d+)= Match every thing up to = but capture digits
  • \K Forget matches up to now
  • \d+ Match following digits
  • (?= Start of positive lookahead
    • [\s\S]*? Match anything lazily
    • type3\.value\1= Up to the same value of type3
    • (\d+) Then capture its value in CP #2
  • ) End of positive lookahead

Live demo

The point is matching valueX from a type different than 3 then look for the same valueX from type3. If valueX is hypothetical or there isn't anything special to be looked, then there is no pure approach using regex in a find / replace functionality.

revo
  • 47,783
  • 14
  • 74
  • 117
  • For Notepad++ use: (FzgF(?!84\.)\d{2})\.(\S+)=\S+(?=[\s\S]*?FzgF84\.\2=(\S+)) and substitution \1.\2=\3 since \K does not seem to work. – Harper Aug 30 '18 at 12:47
  • 1
    Notepad++ uses PCRE syntax and `\K` is documented in their docs. [Check it out](http://docs.notepad-plus-plus.org/index.php/Regular_Expressions). – revo Aug 30 '18 at 12:49