Community
    • Login

    switch text

    Scheduled Pinned Locked Moved General Discussion
    10 Posts 5 Posters 1.3k 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.
    • Rushlan KhanR
      Rushlan Khan
      last edited by

      Hello Friends

      Lets say I have a document below with the following entries

      Name: rushlan
      account : 1234
      user: abcd
      Name: Tom
      account: 5678
      user: efgh

      What i would like to do is replace account with user and vice versa. So my correct output will be :

      Name: rushlan
      account : abcd
      user: 1234
      Name: Tom
      account: efgh
      user: 5678

      Thank you
      Rush

      PeterJonesP Terry RT 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Rushlan Khan
        last edited by

        @Rushlan-Khan ,

        Making the assumptions that there is no indentation, and that the extra space between account and : is really there, and that you have windows CRLF newlines:

        • FIND = (?-s)account : (.*)\r\nuser: (.*)$
        • REPLACE = account : $2\r\nuser: $1
        • Search Mode = Regular expression
        • Replace All

        The first set of parens says to make sure . doesn’t match newline; the second the old account name and puts it into capture-group1, the third matches the old user name and stores it into capture-group2. Then the replacement puts capture-group2 with account : prefix and capture-group1 with user: prefix.

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        Rushlan KhanR 1 Reply Last reply Reply Quote 4
        • Terry RT
          Terry R @Rushlan Khan
          last edited by Terry R

          @Rushlan-Khan said in switch text:

          What i would like to do is replace account with user and vice versa. So my correct output will be :

          Since you did not include the examples inside of a “code box” (see the pinned post “Please Read This Before Posting” and the subsequent “Formatting Forum Posts”) your real data may not be exactly as you have portrayed in the example. Nonetheless I have made an assumption that the space between account and the : was in error. This is a regular expression so search mode must be regular expression.

          Find What:(?-s)(account:\x20*)(.+\R)(user:\x20*)(.+\R)
          Replace With:${1}${4}${3}${2}

          Terry

          PS I see @PeterJones got there just before me. You will see we are both very similar, that the beauty of regular expressions. They can achieve the same results with different methods.

          PeterJonesP Rushlan KhanR 2 Replies Last reply Reply Quote 4
          • PeterJonesP
            PeterJones @Terry R
            last edited by

            @Rushlan-Khan ,

            until Terry’s reply, I didn’t notice that one account had a space before the colon and the other didn’t. If I change my assumption to account followed by 0 or 1 space followed by colon, and the replacement always needs to have 0 spaces between account and colon, then it becomes:

            • Find What = (?-s)account\h?: (.*)\r\nuser: (.*)$
            • Replace with = account: $2\r\nuser: $1

            If you added extra groups to mine, like Terry used four groups, then you can preserve whether or not there’s a space in each account :

            • Find What = (?-s)(account\h?: )(.*)\r\n(user: )(.*)$
            • Replace with = $1$4\r\n$3$2

            Please learn from this that giving an accurate representation of the data is critical when asking such questions, because small variations can change the answer. The more effort you put into asking a clean question, the better the answers will be.

            Alan KilbornA 1 Reply Last reply Reply Quote 3
            • Alan KilbornA
              Alan Kilborn @PeterJones
              last edited by Alan Kilborn

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • guy038G
                guy038
                last edited by guy038

                Hello, @rushlan-khan, @peterjones, @terry-R and All,

                Here is a new regex variant which does not care about anything before the ending values to invert :

                SEARCH (?xi) ^ ( account \W+ ) ( \d+ \R ) ( user \W+ ) ( \w+ \R )

                REPLACE \1\4\3\2

                Best Regards,

                guy038

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

                  Hi, @rushlan-khan, @peterjones, @terry-R and All,

                  I did not do it on purpose but my regex S/R allows splitting text, with a line-break, between the names of the fields and their corresponding values !

                  For instance, with this INPUT text :

                  Name: rushlan
                  account :
                   1234
                  user
                  : abcd
                  Name: Tom
                  account: 5678
                  user: efgh
                  

                  and the regex S/R :

                  SEARCH (?xi) ^ ( account \W+ ) ( \d+ \R ) ( user \W+ ) ( \w+ \R )

                  REPLACE \1\4\3\2

                  it would correctly return this OUTPUT text :

                  Name: rushlan
                  account :
                   abcd
                  user
                  : 1234
                  Name: Tom
                  account: efgh
                  user: 5678
                  

                  Why : just because the regex syntax \W, equivalent to [^\w], means any character which is not a word char ! So this char may also be a line-break character ( \r or \n )

                  Best Regards,

                  guy038

                  Rushlan KhanR 1 Reply Last reply Reply Quote 3
                  • Rushlan KhanR
                    Rushlan Khan @guy038
                    last edited by

                    @guy038 WOW!! thank you so much . Let me absorb this, I will get a back to you if I have any questions.

                    1 Reply Last reply Reply Quote 0
                    • Rushlan KhanR
                      Rushlan Khan @Terry R
                      last edited by

                      @Terry-R thanks for your help !! Sorry this is my first time here, I will read the rules for sure now.

                      1 Reply Last reply Reply Quote 0
                      • Rushlan KhanR
                        Rushlan Khan @PeterJones
                        last edited by

                        @PeterJones Thanks for your help !

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