Community
    • Login

    Replace entire line based on first characters

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 4 Posters 1.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      SilverMew22
      last edited by

      Hello, how do I replace every line that starts with “X”: 50. with “X”: 50.0,
      There are random numbers after every “X”: 50. and I want them to be set to 0

      Here is the document I’m working on (I’ve removed some lines to fit in the post). I’ve tried doing it one by one, but I’m hoping someone could help me find a shortcut.

      {
      “Events”: [
      {
      “Delta”: 0,
      “EventType”: “MouseDown”,
      “Timestamp”: 603,
      “X”: 50.0,
      “Y”: 79.61000061035056
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 683,
      “X”: 50.0,
      “Y”: 79.50000213623050
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 687,
      “X”: 50.0,
      “Y”: 78.93000030507578
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 699,
      “X”: 50.0,
      “Y”: 78.26000213623050
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 703,
      “X”: 50.0,
      “Y”: 76.9000015258789
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 716,
      “X”: 50.0,
      “Y”: 75.26000213623050
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 719,
      “X”: 50.0,
      “Y”: 73.0
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 730,
      “X”: 50.0,
      “Y”: 71.0
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 891,
      “X”: 50.880001068115234,
      “Y”: 41.540000915527344
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 895,
      “X”: 50.77000045776367,
      “Y”: 40.66999816894531
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 906,
      “X”: 50.720001220703125,
      “Y”: 39.5099983215332
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 911,
      “X”: 50.61000061035056,
      “Y”: 38.55099923706055
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 923,
      “X”: 50.61000061035056,
      “Y”: 38.060001373291016
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 926,
      “X”: 50.61000061035056,
      “Y”: 37.38999938965044
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 939,
      “X”: 50.5,
      “Y”: 36.61000061035056
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 943,
      “X”: 50.45000076293945,
      “Y”: 35.84000015258789
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 955,
      “X”: 50.34000015258789,
      “Y”: 35.15999985041211
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 959,
      “X”: 50.34000015258789,
      “Y”: 34.5000016784668
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 971,
      “X”: 50.34000015258789,
      “Y”: 33.709999084502656
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 975,
      “X”: 50.34000015258789,
      “Y”: 33.0
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 986,
      “X”: 50.34000015258789,
      “Y”: 32.16999816894531
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 991,
      “X”: 50.34000015258789,
      “Y”: 31.399999618530273
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 1003,
      “X”: 50.34000015258789,
      “Y”: 30.719999313354502
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 1007,
      “X”: 50.34000015258789,
      “Y”: 30.0
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 1019,
      “X”: 50.34000015258789,
      “Y”: 29.3700008392334
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 1023,
      “X”: 50.34000015258789,
      “Y”: 28.690000534057617
      },
      {
      “Delta”: 0,
      “EventType”: “MouseMove”,
      “Timestamp”: 1886,
      “X”: 50.95000076293945,
      “Y”: 6.659999850412109
      },
      {
      “Delta”: 0,
      “EventType”: “MouseUp”,
      “Timestamp”: 1950,
      “X”: 50.95000076293945,
      “Y”: 6.659999850412109
      }
      ]
      }

      Mark OlsonM mkupperM 2 Replies Last reply Reply Quote 1
      • Mark OlsonM
        Mark Olson @SilverMew22
        last edited by Mark Olson

        @SilverMew22

        In the future, please wrap text samples inside of ``` so that they show up like this:

        {"this is the proper way to display text": true}
        

        In any case, you are working with JSON. Other people might suggest using regular expressions. This is a bad idea.

        I recommend installing the JsonTools plugin and running the following query: @.Events[:].X = 50.0

        Once you run that query, every X value should be converted to 50.0, and the document will also be pretty-printed.

        This query converts the X field of each element in the Events field of the root JSON to the number 50.0. To learn more about the syntax of this query, I recommend reading this documentation on the RemesPath query language.

        That query assumes that the structure of the file is more or less as you showed it. If that query raises an error, I will need to know more about the structure of your JSON.

        1 Reply Last reply Reply Quote 1
        • mkupperM
          mkupper @SilverMew22
          last edited by

          @SilverMew22 said in Replace entire line based on first characters:

          Hello, how do I replace every line that starts with “X”: 50. with “X”: 50.0,
          There are random numbers after every “X”: 50. and I want them to be set to 0

          Do it as a regular expression search/replace
          Search: (?-i)^(\s*"X":\s*50\.)\d*
          Replace: \1\x30

          Parsing that from left to right:

          • (?-i) - Make this a case-sensitive search/replace so that “X” does not match “x”.
          • ^ - Start at the beginning of a line.
          • (\s*"X":\s*50\.) - This is a capture group as it’s inside the parentheses. It contains:
          • \s* - Match zero or more spaces/tabs at the beginning of the line.
          • "X":\s*50\. - Match the characters "X": followed by \s* zero or more spaces. followed by the characters 50..
          • \d* - match zero or more digits

          The replacement has:

          • \1 - this is the contents of the first capture group and will be the leading spaces followed by the `“X”: 50.’ that was matched and stored in the capture group.
          • \x30 - This is an ASCII zero. I could have used a replacement of \10 but that sure looks like a ten and so I used \x30 instead of 0 to reduce the chances of confusion.

          This will change a 50. without any following digits into 50.0. If you don’t desire that then this can be fixed.

          This also “replaces” 50.0 with 50.0 which is no change but it made the search/replace expression simpler.

          I decided to ignore the comma at the end of the “X” lines. If it’s there then it remains there.

          As @Mark-Olson noted, this should really be handled by something that is JSON aware. If by chance you have “X” elements inside of things other than the blocks you have in your example then those too will have a 50.... value changed to 50.0.

          1 Reply Last reply Reply Quote 1
          • S
            SilverMew22
            last edited by

            @mkupper said in Replace entire line based on first characters:

            \1\x30

            Thank you so much, it worked. You saved me hours of time.

            1 Reply Last reply Reply Quote 0
            • Mark OlsonM
              Mark Olson
              last edited by

              One change I would make to the regex of mkupper, if you’re going to use regular expressions:
              replace (?-i)((?<!\\)"X"\s*:\s*50\.)\d* with ${1}0

              Essentially the same, except:

              • I get rid of the ^\s* before the “X” key, and allow any amount of whitespace between the key and the colon, because JSON-parsing regexes should not make any assumptions about formatting.
              • The (?<!\\) before the open quote is just to guard against any weirdness with escaped quotes
              • I use the alternate ${1} syntax to get the first capture group in the replacement regex, because I like that syntax better.
              1 Reply Last reply Reply Quote 1
              • guy038G
                guy038
                last edited by guy038

                Hello, @silvermew22, @mkupper, @mark-olson and All,

                I suppose that the following version should be better as it reduces the replacements to the syntaxes like 50.880001068115234 or 50.5 only !

                SEARCH (?-i)((?<!\\)"X"\s*:\s*50\.)(?!0,)\d+

                Replace ${1}0


                Now, in order to only get modifications, in the right "Events" array, we could use this version :

                SEARCH (?-i)(?:"Events"|(?!\A)\G)(?s:(?!\x5D).)*?\K((?<!\\)"X"\s*:\s*50\.)(?!0,)\d+

                REPLACE ${1}0

                So :

                • Paste your JSON file OR the example, below, in a new tab
                {
                "Tests": [
                {
                "Delta": 0,
                "EventType": "MouseDown",
                "Timestamp": 603,
                "X": 50.0,
                "Y": 79.61000061035056
                },
                {
                "Delta": 0,
                "EventType": "MouseMove",
                "Timestamp": 891,
                "X": 50.880001068115234,
                "Y": 41.540000915527344
                },
                {
                "Delta": 0,
                "EventType": "MouseMove",
                "Timestamp": 939,
                "X": 50.5,
                "Y": 36.61000061035056
                },
                ]
                },
                {
                "Events": [
                {
                "Delta": 0,
                "EventType": "MouseDown",
                "Timestamp": 603,
                "X": 50.0,
                "Y": 79.61000061035056
                },
                {
                "Delta": 0,
                "EventType": "MouseMove",
                "Timestamp": 683,
                "X": 50.0,
                "Y": 79.50000213623050
                },
                {
                "Delta": 0,
                "EventType": "MouseMove",
                "Timestamp": 891,
                "X": 50.880001068115234,
                "Y": 41.540000915527344
                },
                {
                "Delta": 0,
                "EventType": "MouseMove",
                "Timestamp": 939,
                "X": 50.5,
                "Y": 36.61000061035056
                },
                {
                "Delta": 0,
                "EventType": "MouseUp",
                "Timestamp": 1950,
                "X": 50.95000076293945,
                "Y": 6.659999850412109
                }
                ]
                }
                
                • Move the caret to the very beginning of the file ( Very IMPORTANT )

                • Open the Replace dialog ( Ctrl + H )

                • Untick all the box options

                • Click once on the Replace All button

                Voila ! There were only 3 replacements in the unique "Events" array… and none in the "Tests" array !

                Best Regards,

                guy038

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors