Community
    • Login

    regex search replace

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    12 Posts 6 Posters 730 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.
    • Thomas-OlivierT
      Thomas-Olivier @Mark Olson
      last edited by

      I have this pattern:
      air(bag|frame|worthiness)

      and I would like to receive:
      (airbag|airframe|airworthiness)

      Thomas-OlivierT 1 Reply Last reply Reply Quote 0
      • Thomas-OlivierT
        Thomas-Olivier @Thomas-Olivier
        last edited by

        @Thomas-Olivier

        or:
        I have this pattern:
        (aff|ch|disrep)air

        and I would like to receive:
        (affair|chair|disrepair)

        mkupperM 1 Reply Last reply Reply Quote 0
        • mkupperM
          mkupper @Thomas-Olivier
          last edited by

          @Thomas-Olivier The answer provided by @Mark-Olson works for all three of your questions here.

          If Mark Olson’s answer does not work for you then you will need rephrase the question and add more detail. In the past you would ask questions here but then say the help provided did not work as you are using another editor. If that is still the case then seek help in a forum that supports that other editor. Not all rexex parsers are the same.

          Thomas-OlivierT 1 Reply Last reply Reply Quote 1
          • Thomas-OlivierT
            Thomas-Olivier @mkupper
            last edited by

            It’s just 3 words in abbreviated form for Regex.
            I want to get them in full form… 3 full words in Notepad++.
            I understand… too difficult.
            No suggestions here either.

            Thank you for your answer.

            1 Reply Last reply Reply Quote 0
            • CoisesC
              Coises
              last edited by

              If I understand what you want, I do not think it can be done with regular expressions.

              You have data of the form:

              Prefix(Suffix1|Suffix2|Suffix3)

              and you want to use Search/Replace to change that to:

              (PrefixSuffix1|PrefixSuffix2|PrefixSuffix3)

              and the similar case with multiple prefixes and a single suffix. Is that correct? (I think others are misunderstanding you because the parenthesis and vertical bars make it look like you’re presenting regular expressions, but I believe you mean that is the actual form of your data.)

              While I don’t think it can be done, I never want to underestimate the power of @guy038 to do the seemingly impossible with a regular expression.

              Mark OlsonM 1 Reply Last reply Reply Quote 2
              • Mark OlsonM
                Mark Olson @Coises
                last edited by Mark Olson

                @Coises
                Huh. Yeah, maybe that’s what he’s saying. In that case, there are two regex-replaces that do the job:

                1. To convert (aff|ch|disrep|fl|st)air into (affair|chair|disrepair|flair|stair):
                  • FIND: (?-s)(\w+)(?=.*\)(\w+)$)|(\w+$)
                  • REPLACE WITH: ?3:$1$2
                2. To convert her(e|ereafter|on|oine) into (here|herereafter|heron|heroine) is a two-step process:
                  1. First convert it into (e|ereafter|on|oine)her with:
                    • FIND: (?-s)(^\w+)(.+)
                    • REPLACE WITH: $2$1
                  2. Convert it into (here|herereafter|heron|heroine) with:
                    • FIND: (?-s)(\w+)(?=.*\)(\w+)$)|(\w+$)
                    • REPLACE WITH: ?3:$2$1

                The second case is a lot harder because there is variable-length lookahead but not variable-length lookbehind.

                1 Reply Last reply Reply Quote 2
                • guy038G
                  guy038
                  last edited by guy038

                  Hello, @thomas-olivier, @mark-olson, @coises and All,

                  @thomas-olivier, starting with your two needs below :

                  air(bag|frame|worthiness)    =>    (airbag|airframe|airworthiness)
                  
                  (aff|ch|disrep)air           =>    (affair|chair|disrepair)
                  

                  I succeeded to find out a set of 4/5 consecutive regexes which do the job nicely ! I assume that:

                  • You first select the complete zone to modify ( for instance, the string air(bag|frame|worthiness) or the string (aff|ch|disrep)air )

                  • Each time, you must run the five or four set of regexes, below, on this selection ONLY

                                 Prefix                                                         Suffix
                  
                  (?-s)(.+)(\(.+?\))                -->    \2\1                                N/A
                                                                            
                  (\W)([\w ]+)                      -->    \1\r\n\2              (\W)([\w ]+)                      -->    \1\r\n\2
                  
                  [\w ]+(?=(?s).*^([\w ]+))         -->    \1$0                  [\w ]+(?=(?s).*^([\w ]+))         -->    $0\1
                  
                  \R                                -->    EMPTY                 \R                                -->    EMPTY   
                  
                  (?s)(.+\)).+                      -->    \1                    (?s)(.+\)).+                      -->    \1      
                  
                  

                  It’s better to include these two bunch of regexes in two macros, respectively called Prefix and Suffix, below :

                          <Macro name="Prefix" Ctrl="no" Alt="no" Shift="no" Key="0">
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="(.+)(\(.+?\))" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\2\1" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="(\W)([\w ]+)" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1\r\n\2" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="[\w ]+(?=(?s).*^([\w ]+))" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1$0" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="\R" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?s)(.+\)).+" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                          </Macro>
                  
                          <Macro name="Suffix" Ctrl="no" Alt="no" Shift="no" Key="0">
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="(\W)([\w ]+)" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1\r\n\2" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="[\w ]+(?=(?s).*^([\w ]+))" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="$0\1" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="\R" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                  
                              <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                              <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?s)(.+\)).+" />
                              <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                              <Action type="3" message="1602" wParam="0" lParam="0" sParam="\1" />
                              <Action type="3" message="1702" wParam="0" lParam="130" sParam="" />
                              <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                          </Macro>
                  

                  Just place these above macros in your active shortcuts.xml file !


                  So the process is :

                  • Select a complete zone, defined as PREFIX(aaaa|bbbb|....|yyyy|zzzz) or defined as (aaaa|bbbb|....|yyyy|zzzz)SUFFIX

                  • Run the appropriate macro ( Prefix or Suffix )

                  Here you are !!

                  Notes :

                  • These macros allows to define multi-words as a single zone

                  • These macros work whatever the number of items between parentheses !

                  • If no selection is set, these macros do nothing and text, at caret location, stays unchanged


                  Some examples :

                  • From this INPUT text :
                  This is a	first TRY(word1|WORD2|This is a multi word3|word4)    small test
                  

                  After selecting the string first TRY(word1|WORD2|This is a multi word3|word4) and running the Prefix macro, we get the following OUTPUT text :

                  This is a	(first TRYword1|first TRYWORD2|first TRYThis is a multi word3|first TRYword4)    small test
                  
                  • From this INPUT text :
                  This is a	(word1|WORD2|This is a multi word3|word4)SECOND try    small test
                  

                  After selecting the string (word1|WORD2|This is a multi word3|word4)SECOND try and running the Suffix macro, we’re left with thuis OUTPUT text :

                  This is a	(word1SECOND try|WORD2SECOND try|This is a multi word3SECOND try|word4SECOND try)    small test
                  
                  • Finally, from this INPUT text :
                  FIRST_(aaaaa|bbbbb|fff ggg hhh iii jjj|yyyyy|zzzzz)_LAST
                  
                  • First select the zone FIRST_(aaaaa|bbbbb|fff ggg hhh iii jjj|yyyyy|zzzzz) ( do not include the last part _LAST )

                  • Run the Prefix macro

                  We obtain this temporary text :

                  (FIRST_aaaaa|FIRST_bbbbb|FIRST_fff ggg hhh iii jjj|FIRST_yyyyy|FIRST_zzzzz)_LAST
                  
                  • Now, select all the zone (FIRST_aaaaa|FIRST_bbbbb|FIRST_fff ggg hhh iii jjj|FIRST_yyyyy|FIRST_zzzzz)_LAST

                  • Run the Suffix macro

                  Here is our expected OUTPUT text :

                  (FIRST_aaaaa_LAST|FIRST_bbbbb_LAST|FIRST_fff ggg hhh iii jjj_LAST|FIRST_yyyyy_LAST|FIRST_zzzzz_LAST)
                  

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 1
                  • Thomas-OlivierT
                    Thomas-Olivier
                    last edited by Thomas-Olivier

                    Thanks to Mark Olson.
                    It works flawlessly.

                    Now I want to test the macro but there are complications.

                    @guy038
                    I have 2 shortcuts.xml files.

                    1. D:\Notepad++\shortcuts.xml
                    2. D:\Notepad++\Users\Administrator\AppData\Roaming\Notepad++\shortcuts.xml

                    '1.
                    <NotepadPlus>
                    <InternalCommands />
                    <Macros>
                    <Macro name=“Trim Trailing and save” Ctrl=“no” Alt=“yes” Shift=“yes” Key=“83”>
                    <Action type=“2” message=“0” wParam=“42024” lParam=“0” sParam=“” />
                    <Action type=“2” message=“0” wParam=“41006” lParam=“0” sParam=“” />
                    </Macro>
                    </Macros>
                    <UserDefinedCommands>
                    <Command name=“Launch in Firefox” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“88”>firefox “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in IE” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“73”>iexplore “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in Chrome” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“82”>chrome “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in Safari” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“65”>safari “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Get php help” Ctrl=“no” Alt=“yes” Shift=“no” Key=“112”>http://www.php.net/$(CURRENT_WORD)</Command>
                    <Command name=“Wikipedia Search” Ctrl=“no” Alt=“yes” Shift=“no” Key=“114”>https://en.wikipedia.org/wiki/Special:Search?search=$(CURRENT_WORD)</Command>
                    <Command name=“Open file in another instance” Ctrl=“no” Alt=“yes” Shift=“no” Key=“117”>$(NPP_FULL_FILE_PATH) $(CURRENT_WORD) -nosession -multiInst</Command>
                    <Command name=“Send via Outlook” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“79”>outlook /a “$(FULL_CURRENT_PATH)”</Command>
                    </UserDefinedCommands>
                    <PluginCommands />
                    <ScintillaKeys />
                    </NotepadPlus>

                    comment: there is only closure <PluginCommands />

                    <NotepadPlus>
                    <InternalCommands>
                    <Shortcut id=“47000” Ctrl=“no” Alt=“no” Shift=“no” Key=“0” />
                    <Shortcut id=“43008” Ctrl=“no” Alt=“no” Shift=“no” Key=“27” />
                    <Shortcut id=“41003” Ctrl=“no” Alt=“no” Shift=“no” Key=“0” />
                    </InternalCommands>
                    <Macros>
                    <Macro name=“Trim Trailing and save” Ctrl=“no” Alt=“yes” Shift=“yes” Key=“83”>
                    <Action type=“2” message=“0” wParam=“42024” lParam=“0” sParam=“” />
                    <Action type=“2” message=“0” wParam=“41006” lParam=“0” sParam=“” />
                    </Macro>
                    <Macro name=“skok do pustej linii” Ctrl=“no” Alt=“no” Shift=“no” Key=“112”>
                    <Action type=“3” message=“1700” wParam=“0” lParam=“0” sParam=“” />
                    <Action type=“3” message=“1601” wParam=“0” lParam=“0” sParam=“\r\n\r\n” />
                    <Action type=“3” message=“1625” wParam=“0” lParam=“1” sParam=“” />
                    <Action type=“3” message=“1702” wParam=“0” lParam=“512” sParam=“” />
                    <Action type=“3” message=“1701” wParam=“0” lParam=“1” sParam=“” />
                    </Macro>
                    </Macros>
                    <UserDefinedCommands>
                    <Command name=“Launch in Firefox” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“88”>firefox “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in IE” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“73”>iexplore “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in Chrome” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“82”>chrome “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Launch in Safari” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“65”>safari “$(FULL_CURRENT_PATH)”</Command>
                    <Command name=“Get php help” Ctrl=“no” Alt=“yes” Shift=“no” Key=“112”>http://www.php.net/$(CURRENT_WORD)</Command>
                    <Command name=“Wikipedia Search” Ctrl=“no” Alt=“yes” Shift=“no” Key=“114”>https://en.wikipedia.org/wiki/Special:Search?search=$(CURRENT_WORD)</Command>
                    <Command name=“Open file in another instance” Ctrl=“no” Alt=“yes” Shift=“no” Key=“117”>$(NPP_FULL_FILE_PATH) $(CURRENT_WORD) -nosession -multiInst</Command>
                    <Command name=“Send via Outlook” Ctrl=“yes” Alt=“yes” Shift=“yes” Key=“79”>outlook /a “$(FULL_CURRENT_PATH)”</Command>
                    </UserDefinedCommands>
                    <PluginCommands>
                    <PluginCommand moduleName=“DSpellCheck.dll” internalID=“0” Ctrl=“no” Alt=“yes” Shift=“no” Key=“78” />
                    </PluginCommands>
                    <ScintillaKeys>
                    <ScintKey ScintID=“2325” menuCmdID=“0” Ctrl=“no” Alt=“no” Shift=“no” Key=“0” />
                    </ScintillaKeys>
                    </NotepadPlus>

                    I haven’t used macro yet.
                    Unnecessary entries can be deleted.

                    In which shortcuts.xml should I insert this macro now?

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @Thomas-Olivier
                      last edited by

                      @Thomas-Olivier said in regex search replace:

                      In which shortcuts.xml should I insert this macro now?

                      Likely the “appdata” one, but to know for sure we’d have to see your “Debug Info” (see the “?” menu).

                      1 Reply Last reply Reply Quote 1
                      • Thomas-OlivierT
                        Thomas-Olivier
                        last edited by

                        I put it in appdata and the one from the main D:\Notepad++\shortcuts.xml I removed.

                        A big thank you to @guy038. It works flawlessly.

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