Community
    • 登入

    Replace specific word between words in string

    已排程 已置頂 已鎖定 已移動 General Discussion
    17 貼文 4 Posters 4.0k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Program111P
      Program111
      最後由 Program111 編輯

      此回覆已被刪除!
      Alan KilbornA 1 條回覆 最後回覆 回覆 引用 0
      • Alan KilbornA
        Alan Kilborn @Program111
        最後由 編輯

        @Program111

        In the Replace window, put , TRUE in the Find what box.
        In the Replace with box, make sure this box is empty.
        Press the Replace All button.
        Done.

        1 條回覆 最後回覆 回覆 引用 1
        • Program111P
          Program111
          最後由 編輯

          I have the word TRUE in other fields that does not start like this line. I need regular expression.

          1 條回覆 最後回覆 回覆 引用 0
          • guy038G
            guy038
            最後由 guy038 編輯

            Hello, @program111, and All,

            The @alan-kilborn’s solution is valid but, just in case you’ve got some other zones , True in your file, here is a safer solution :

            • Open the Replace dialog ( Ctrl + F )

            • SEARCH (?-si)define\(".+?\K,\x20TRUE(?=\);)

            • REPLACE Leave EMPTY

            • Tick the Wrap around option

            • Select the Regular expression search mode

            • Click on the Replace All button, exclusively

            Notes :

            • Globally this regex searches, first, for the literal zone define(", with this exact case. followed by the shortest non-null range of standard chars. till the string ,\x20TRUE, with that exact case, but ONLY IF followed with the string );

            • The (?-is) in-line modifiers forces a non-insensitive search and a non-single line text, meaning that text is split in individual lines and that the regex meta-character . matches a single standard character only ( and not line-breaks )

            • The parentheses have a special regex meaning and must be escaped to be considered as literals

            • The \K syntax causes the regex engine to only retain the part , TRUE, which is deleted as the replacement zone is empty

            Best Regards,

            guy038

            1 條回覆 最後回覆 回覆 引用 1
            • PeterJonesP
              PeterJones
              最後由 PeterJones 編輯

              For future readers who (like me) were lost/confused because the first post was deleted, the OP apparently posted the same (or similar) question in this linked post.

              Alan KilbornA 1 條回覆 最後回覆 回覆 引用 1
              • Alan KilbornA
                Alan Kilborn @PeterJones
                最後由 編輯

                @PeterJones said in Replace specific word between words in string:

                for future readers who (like me) were lost/confused because the first post was deleted, the OP apparently posted the same (or similar) question in this linked post.

                The OP was probably shocked by their original question once they saw my “solution”…that they had to abandon the thread entirely!! :-)

                1 條回覆 最後回覆 回覆 引用 0
                • Program111P
                  Program111
                  最後由 Program111 編輯

                  @Alan-Kilborn no I did not. I was shocked that someone would think other person to be so stupid to post simple FIND and REPLACE question. I try to edit the question and it did not allow. so I posted new one and clarified it.
                  @guy038 I still did not get your solution. just tell me one line reply and solution pleae.

                  PeterJonesP 1 條回覆 最後回覆 回覆 引用 1
                  • PeterJonesP
                    PeterJones @Program111
                    最後由 編輯

                    @Program111,

                    @guy038 I still did not get your solution. just tell me one line reply and solution pleae.

                    By “did not get”, do you mean you did not receive (“get”) the solution that Guy has posted in this thread, or that you did not understand (“get”) the solution. In the first 6 bullet points (before the Notes), Guy told you the exact steps you need to take to run this search-and-replace. The Notes were just Guy’s way of helping you learn what those 6 steps do.

                    Did you try those 6 steps? If not, why not? If so, did it work for you? If it didn’t work, did you follow his instructions to use Replace All exclusively (not hit the Replace button) – because with \K in the regular expression, you must use Replace All to work. If it’s not working for you, what results did it give you, compared to what results were you expecting?

                    Please follow the advice below for how to format your post, present example text, etc.

                    ----

                    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

                    1 條回覆 最後回覆 回覆 引用 1
                    • Program111P
                      Program111
                      最後由 編輯

                      @guy038 said in Replace specific word between words in string:

                      Wrap around option

                      where is step 6? the “Replace All”? NO I did not .
                      I don’t know what to put int he “replace” field to remove the “, TRUE);”

                      Alan KilbornA 1 條回覆 最後回覆 回覆 引用 0
                      • Alan KilbornA
                        Alan Kilborn @Program111
                        最後由 編輯

                        @Program111 said in Replace specific word between words in string:

                        where is step 6? the “Replace All”?

                        3e313835-987c-476b-bc16-fc1d116970db-image.png

                        I don’t know what to put int he “replace” field

                        d82649bc-cd39-4de2-a308-71e352829c9d-image.png

                        Perhaps “Leave EMPTY” is bad terminology, if it wasn’t empty in the first place. Instead try:

                        • put caret in Replace with box
                        • press Ctrl+a (to select all)
                        • press Delete
                        PeterJonesP 1 條回覆 最後回覆 回覆 引用 2
                        • guy038G
                          guy038
                          最後由 編輯

                          Hi, @program111, and All,

                          Oh, my bad ! I was mistaken in my previous post ! I wrote :

                          • Open the Replace dialog ( Ctrl + F )

                          I should have written :

                          • Open the Replace dialog ( Ctrl + H )

                          Sorry,

                          BR

                          guy038

                          1 條回覆 最後回覆 回覆 引用 1
                          • PeterJonesP
                            PeterJones @Alan Kilborn
                            最後由 編輯

                            @Program111 ,

                            Maybe a picture will help:

                            5aab12b4-8d3d-498b-bc56-f87409e22a87-image.png

                            If you start with this data:

                            define("EW_UNFORMAT_YEAR", 50, TRUE);
                            define("EW_UNFORMAT_YEAR", 51, TRUE);
                            define("EW_UNFORMAT_YEAR", 52, TRUE);
                            define("EW_UNFORMAT_YEAR", 53, TRUE);
                            define("EW_UNFORMAT_YEAR", 54, TRUE);
                            define("EW_UNFORMAT_YEAR", 55, TRUE);
                            define("EW_UNFORMAT_YEAR", 56, TRUE);
                            define("EW_UNFORMAT_YEAR", 57, TRUE);
                            define("EW_UNFORMAT_YEAR", 58, TRUE);
                            define("EW_UNFORMAT_YEAR", 59, TRUE);
                            define("EW_UNFORMAT_YEAR", 60, TRUE);
                            define("EW_UNFORMAT_YEAR", 61, TRUE);
                            define("EW_UNFORMAT_YEAR", 62, TRUE);
                            

                            After you’ve hit Replace All, you will end up with:

                            define("EW_UNFORMAT_YEAR", 50);
                            define("EW_UNFORMAT_YEAR", 51);
                            define("EW_UNFORMAT_YEAR", 52);
                            define("EW_UNFORMAT_YEAR", 53);
                            define("EW_UNFORMAT_YEAR", 54);
                            define("EW_UNFORMAT_YEAR", 55);
                            define("EW_UNFORMAT_YEAR", 56);
                            define("EW_UNFORMAT_YEAR", 57);
                            define("EW_UNFORMAT_YEAR", 58);
                            define("EW_UNFORMAT_YEAR", 59);
                            define("EW_UNFORMAT_YEAR", 60);
                            define("EW_UNFORMAT_YEAR", 61);
                            define("EW_UNFORMAT_YEAR", 62);
                            

                            If you ignore this advice, and just hit Replace, it won’t work.

                            Alan KilbornA 1 條回覆 最後回覆 回覆 引用 1
                            • Alan KilbornA
                              Alan Kilborn @PeterJones
                              最後由 編輯

                              @PeterJones

                              I suppose it is easy enough to remove the Replace All requirement, with this solution:

                              Open the Replace dialog by pressing Ctrl+h and then set up the following search parameters:
                              Find what box: (?-si)(define\(".+?),\x20TRUE(?=\);)
                              Replace with box: \1);
                              Search mode radiobutton: Regular expression
                              Wrap around checkbox: ticked
                              In selection checkbox: unticked
                              Option checkboxes not mentioned are typically not important to the operation, but should in general be unticked.
                              Then press the Replace button, one or many times.

                              I typically don’t like to provide \K in solutions posted here, due to the Replace All -being-needed limitation. Of course, sometimes \K is absolutely required for a successful solution, but in this case using it only buys you the need to not capture group anything and the ability to leave the Replace with field empty (which in itself can be confusing: it looks empty but maybe it has some spaces in it).

                              1 條回覆 最後回覆 回覆 引用 1
                              • guy038G
                                guy038
                                最後由 編輯

                                Hello, @program111, @peterjones, @alan-kilborn and All,

                                @alan-kilborn :

                                Thanks for proposing the right solution ! I confess that I’m rather annoying to always look for the minimum S/R syntax :-(

                                I realize, finally, that the benefit of such a practice is really small ! Indeed, by removing the \K syntax and adding the ( ), to create the group that contains everything that matched before \K, the search string contains the same number of characters, anyway ! Only the replacement field contains a few elements, instead of an empty field !

                                But the fact that the Replace button is fully functional is surely less confusing for some OP, who are not aware of some specificities of our regex engine :-))

                                Best Regards,

                                guy038

                                Alan KilbornA 1 條回覆 最後回覆 回覆 引用 1
                                • Alan KilbornA
                                  Alan Kilborn @guy038
                                  最後由 編輯

                                  @guy038

                                  Do we have any further insight as to why \K doesn’t work with normal Replace?
                                  I thought maybe since some time has gone by since it was last discussed…

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • guy038G
                                    guy038
                                    最後由 guy038 編輯

                                    Hi, @alan-kilborn,

                                    I’ve just tested with the old enhanced regex engine of François-R Boyer + Notepad++ v6.9 and, unfortunately, the behaviour is strictly identical : any hit on the Replace button does not produce anything :-((

                                    But it’s definitively a bug !

                                    Cheers,

                                    guy038

                                    1 條回覆 最後回覆 回覆 引用 2
                                    • Program111P
                                      Program111
                                      最後由 編輯

                                      @Alan-Kilborn said in Replace specific word between words in string:

                                      \1);

                                      @Alan-Kilborn @PeterJones @guy038 thank you for your help. it worked. I appreciate it.

                                      1 條回覆 最後回覆 回覆 引用 2
                                      • 第一個貼文
                                        最後的貼文
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors