开发者

RegEx to reverse order of list?

开发者 https://www.devze.com 2023-01-02 04:13 出处:网络
Is there a singular regular expression that can be used in, say, a text editor\'s search/replace dialog to reverse the order of the items in a list?

Is there a singular regular expression that can be used in, say, a text editor's search/replace dialog to reverse the order of the items in a list?

For instance, take this list:

  • First item
  • Second item
  • Third item

Select it in a text editor like EditPad, bring up the search and replace box, apply a regex (run as a loop or not) and turn it into:

Can this be done?


This cannot be done by a regular expression.

I'd recommend using a language like Perl, where you can use a regular expression to split the list and write it back in reversed order.


In MSDOS

SORT /R < originallist.txt > resortedlist.txt


Only if the list has a fixed, known number n of items, and even then the regexp gets more complicated as n grows. (The main difficulty is usually to get a literal newline into the engine.)

It is possible to generate this series of increasingly complex regexes quite easily with a scripting language; however, once you use a scripting language, it is almost certainly easier to use it for the reversing itself!


I realize it's been quite some time since this was asked... but this answer wasn't here.

Depending on how you've formatted your list within the text editor you could do something along these lines:

Original text: "The Quick Brown Fox"

Find: (\w+)\s(\w+)\s(\w+)\s(\w+)

Replace: $4 $3 $2 $1

Output: "Fox Brown Quick The" (works on notepad++, just make sure you select the option for "regular expression")

equivalent to the javascript:

        var string = "The Quick Brown Fox";
        string.replace(/(\w+)\s(\w+)\s(\w+)\s(\w+)/,"$4 $3 $2 $1");

good reference is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions


Just stumbled on this question, as I was trying to do the same in Google Sheets.

It turns out you can reverse a list with a regex there using the regexreplace function, as long as you know the max length of the list. Note that Sheets can take the output of one regex conversion and feed it into another one, which is key because you need to nest O(log N) of them. Also, if the regex doesn't match, the regexreplace output equals the input, which is also key for it to work on any length list up to the max. And, you need to change the delimiter, but that could be changed back in a final wrapper if you want.

For example, suppose you have a list of lower case words, separated by commas: "one,two,three,four,five"

Then, using Google Sheets regexreplace function and regex syntax (it uses RE2 regexes, so this should (?) translate to most languages), the following will do it (indenting added for readability):

=regexreplace(
    regexreplace(
        regexreplace(A1,"([a-z\|]+),([a-z\|]+)","$2|$1"),
    "([a-z\|]+),([a-z\|]+)","$2|$1"),
"([a-z\|]+),([a-z\|]+)","$2|$1")

The output will be: "five|four|three|two|one". Note the inclusion of the new delimiter '|' in the regex, which is key. This expression will work for up to 8 elements in the list. Add another "regexreplace" and it will work for up to 16, etc.


Not technically regular expressions, but there's a sed one-liner that reverses the lines of input (source):

sed '1!G;h;$!d'

and a vi command to reverse the lines of the current file (source):

:g/.*/m0
0

精彩评论

暂无评论...
验证码 换一张
取 消