regexnotepad++regexp-replace

How to limit/cap values exceeding a certain value in Notepad++


I have this snippet of code in yml:

      - Item: Oldman's_Romance
        Rate: 50
      - Item: Grasshopper's_Leg
        Rate: 8000
      - Item: Azure_Jewel
        Rate: 9000
      - Item: Grasshopper_Doll
        Rate: 1500
      - Item: Angel's_Arrival
        Rate: 1000
      - Item: Center_Potion
        Rate: 700

I want to change the values of "Rate" to be capped at 5000.

It would look something like this:

      - Item: Oldman's_Romance
        Rate: 50
      - Item: Grasshopper's_Leg
        Rate: 5000
      - Item: Azure_Jewel
        Rate: 5000
      - Item: Grasshopper_Doll
        Rate: 1500
      - Item: Angel's_Arrival
        Rate: 1000
      - Item: Center_Potion
        Rate: 700

Solution

  • If I understood correctly, what you want is to replace all the Rate's that are above 5000 and change them into 5000.

    I think that what you could do is search for

    Rate: [5-9]\d{3}

    And replace to

    Rate: 5000

    Explanation:

    So basically, lets search for strings that has Rate: followed up by a number that must be five to nine, and then other 3 digits

    Note: this only works for numbers above 5000 and below or equal 9999