Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    switch text

    General Discussion
    5
    10
    97
    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 Khan
      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

      PeterJones Terry R 2 Replies Last reply Reply Quote 0
      • PeterJones
        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 Khan 1 Reply Last reply Reply Quote 4
        • Terry R
          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.

          PeterJones Rushlan Khan 2 Replies Last reply Reply Quote 4
          • PeterJones
            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 Kilborn 1 Reply Last reply Reply Quote 3
            • Alan Kilborn
              Alan Kilborn @PeterJones last edited by Alan Kilborn

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • guy038
                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
                • guy038
                  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 Khan 1 Reply Last reply Reply Quote 3
                  • Rushlan Khan
                    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 Khan
                      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 Khan
                        Rushlan Khan @PeterJones last edited by

                        @PeterJones Thanks for your help !

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Copyright © 2014 NodeBB Forums | Contributors