How can we reverse words in a row



  • Hello, I’ve a list of words organized this way:

    word1|word2
    word3|word4

    I want to reverse rows this way:

    word2|word1
    word4|word3

    Can we do this through notepad++? If yes, could you please tell me how?

    Thanks!



  • @Mou-Nir

    Can we do this through notepad++?

    Yes, using the numbered capture group feature.

    • FIND = (\w+)\|(\w+) – store the word before the | as group#1, and after as group#2
    • REPLACE = ${2}|${1} – in the replacement, use group#2 first, then |, then group#1
    • Search Mode = regular expression

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • Thank you sir peter for the help, but I’ve faced a small issue which is as follow:

    for instance, word1 word1|word2 , only word1 which close to the separator (|) gets selected by this code (\w+)|

    please, what should I do in such situation? Thanks again



  • @Mou-Nir

    I think you got what you asked for the first time around, with Peter’s response.
    If you wanted something different, perhaps you should have asked for that.



  • @Alan-Kilborn are you serious? man, how could I know that I would face that issue after trying his code?

    I am here asking for help, It’d better to ask it else where.

    @Alan-Kilborn worst support ever



  • @Mou-Nir said in How can we reverse words in a row:

    Can we do this through notepad++?

    You can also do it much quicker in a spreadsheet. Open the text based delimited file with pipe| as the delimiter and then cut and paste columns to your heart’s delight.

    Sometimes we have to use the right tool for the right job and column manipulation is done much easier and “natively” in a spreadsheet application versus a text editor.

    Cheers.



  • @Mou-Nir said in How can we reverse words in a row:

    @Alan-Kilborn worst support ever

    So, first, there are no “supporters” here in an official sense.
    So a comment like “worst support ever” does not apply. :-)

    Second, if you want something, you should really think about it (what it actually is) before you post. Now, I realize that that is not always a reasonable thing to accomplish…

    However, you provided some data:

    word1|word2
    word3|word4
    

    and your desired result:

    word2|word1
    word4|word3
    

    …which, at first look, is a great thing to have done.

    And Peter provided a solution.

    how could I know that I would face that issue after trying his code?

    But now you say your data is different than you first represented??
    Well, to that I have to comment: “Worst question-asker ever!”
    All right, that’s just a joke, but…

    word1 word1|word2

    Even your changed specification of your data is vague…
    Peter could develop another solution to handle that, and you will say “wait…wait…hold on a minute…I really have data like: __________”



  • @Mou-Nir said in How can we reverse words in a row:

    Thank you sir peter for the help, but I’ve faced a small issue which is as follow:

    You said “word”. You should not be surprised that it didn’t work for multiple “words” on each side of the |.

    Here’s another freebie – but it’s the last from me. The quality of replies is only as good as the quality of your question. If you don’t understand your data well enough to explain it correctly to us, there is no way we would be expected to guess it.

    • FIND = (?-s)^(.*?)\|(.*?)$

    Everything else from my previous solution remains the same.

    This will swap everything on the left of the first | on a line with everything to the right of the first | on the line.

    And, as Alan said, it is difficult to hit a moving target. I have re-aimed once already, and am not likely to do so again.

    If you want to do more fancy search-and-replace in the future, your best bet is going to be to study the documentation I’ve already linked.


Log in to reply