Community
    • Login

    Find start of line up to specific character, and copy this to end of line.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    replace
    3 Posts 2 Posters 57 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.
    • D
      Dad3353
      last edited by

      Fellow Notepad++ Users,

      Could you please help me the the following search-and-replace problem I am having?

      Creating dictionary text for Godot 4, I need to reformat multiple lines of text. All characters up to, but not including, ’ = ’ should be copied to the end of the line, after ‘GlobalVariables.’, and (optional; I could do this part by hand…) the beginning characters should be enclosed in quotes (ie : gv_fire_rang_mini becomes “gv_fire_rang_mini”). I have several hundred lines like these, and more to come as I continue developing my application.
      Thanks in advance; meanwhile… Have a great day.

      Here is the data I currently have (“before” data):

      gv_fire_rang_mini  = GlobalVariables.
      gv_fire_rang_maxi  = GlobalVariables.
      gv_fire_accu_mini  = GlobalVariables.
      gv_fire_accu_maxi  = GlobalVariables.
      gv_fire_dura  = GlobalVariables.
      gv_fire_relo  = GlobalVariables.
      gv_fire_relo_mini  = GlobalVariables.
      

      Here is how I would like that data to look (“after” data):

      "gv_fire_rang_mini"  = GlobalVariables.gv_fire_rang_mini
      "gv_fire_rang_maxi"  = GlobalVariables.gv_fire_rang_maxi
      "gv_fire_accu_mini" = GlobalVariables.gv_fire_accu_mini
      "gv_fire_accu_maxi"  = GlobalVariables.gv_fire_accu_maxi
      "gv_fire_dura"  = GlobalVariables.gv_fire_dura
      "gv_fire_relo"  = GlobalVariables.gv_fire_relo
      "gv_fire_relo_mini"  = GlobalVariables.gv_fire_relo_mini
      

      To accomplish this, I have tried using the following Find/Replace expressions and settings

      • Find What = ^.*(?==\s)
      • Replace With =
      • Search Mode = REGULAR EXPRESSION
      • Dot Matches Newline = NOT CHECKED

      This finds (and deletes…) the start characters, but does not, of course, copy these characters to the end of line. I am too inexperienced with Notepad++ to know if this is even possible. Can it be done…?

      Unfortunately, this did not produce the output I desired, and I’m not sure why. Could you please help me understand what went wrong and help me find the solution?

      gerdb42G 1 Reply Last reply Reply Quote 1
      • gerdb42G
        gerdb42 @Dad3353
        last edited by

        @Dad3353
        Your expression matches everything from the beginning of a line up to (but not including) a “=” followed by whitespace. Then the match is replaced with nothing (effectively removed).

        You will need to tell the RegEx engine what to put in place of the match. In RegEx this is done with “capturing groups”. You may want to look here for more information: https://community.notepad-plus-plus.org/topic/15765/faq-where-to-find-regular-expressions-regex-documentation

        For your need you may try this:

        Find what: (?-s)^(\w+)(.*)$
        Replace with: "$1"$2$1

        (?-s) makes sure that “.” will not match newline, no matter what the checkbox says.
        ^ anchors the expression to the beginning of a line
        (\w+) creates a first capturing group containing all following word characters
        (.*) creates a second capturing group containing the remainder of the line
        $ anchors the expression to the end of the line

        Now for the replace part:
        "$1" inserts the content of the first capturing group surrounded by quotation marks
        $2 inserts the remainder of the line
        $1 inserts the content of the first capturing group again creating your desired copy

        hth

        D 1 Reply Last reply Reply Quote 3
        • D
          Dad3353 @gerdb42
          last edited by

          @gerdb42 Thank you for this very speedy reply and solution; this worked perfectly, with no need for ‘tweaking’ at all. Your explanation is most helpful (I had looked at ‘groups’, but brain fog set in; I’ve just turned 75…).
          One thing I haven’t understood is how the identifying of the first group (up to ‘=’…) works. It can’t count characters, and I don’t see ‘=’ in there as a delimiting character. Is it the ‘space’ that delimits the first group…?
          Either way, this ‘tuition’ has opened up yet another avenue of utility for Notepad++, as if it wasn’t useful enough already, for which I’m very grateful…!

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