• Login
Community
  • Login

switch text

Scheduled Pinned Locked Moved General Discussion
10 Posts 5 Posters 1.4k 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.
  • R
    Rushlan Khan
    last edited by Mar 9, 2023, 9:28 PM

    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

    P T 2 Replies Last reply Mar 9, 2023, 9:34 PM Reply Quote 0
    • P
      PeterJones @Rushlan Khan
      last edited by Mar 9, 2023, 9:34 PM

      @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
      R 1 Reply Last reply Mar 10, 2023, 10:41 PM Reply Quote 4
      • T
        Terry R @Rushlan Khan
        last edited by Terry R Mar 9, 2023, 9:40 PM Mar 9, 2023, 9:38 PM

        @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.

        P R 2 Replies Last reply Mar 9, 2023, 9:54 PM Reply Quote 4
        • P
          PeterJones @Terry R
          last edited by Mar 9, 2023, 9:54 PM

          @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.

          A 1 Reply Last reply Mar 9, 2023, 10:00 PM Reply Quote 3
          • A
            Alan Kilborn @PeterJones
            last edited by Alan Kilborn Mar 9, 2023, 10:01 PM Mar 9, 2023, 10:00 PM

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • G
              guy038
              last edited by guy038 Mar 9, 2023, 10:20 PM Mar 9, 2023, 10:16 PM

              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
              • G
                guy038
                last edited by guy038 Mar 10, 2023, 9:47 AM Mar 10, 2023, 9:37 AM

                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

                R 1 Reply Last reply Mar 10, 2023, 10:36 PM Reply Quote 3
                • R
                  Rushlan Khan @guy038
                  last edited by Mar 10, 2023, 10:36 PM

                  @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
                  • R
                    Rushlan Khan @Terry R
                    last edited by Mar 10, 2023, 10:40 PM

                    @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
                    • R
                      Rushlan Khan @PeterJones
                      last edited by Mar 10, 2023, 10:41 PM

                      @PeterJones Thanks for your help !

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