在记事本中某个字符后对行进行排序 [英] Sort lines after a certain character in notepad++

查看:165
本文介绍了在记事本中某个字符后对行进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几行要从最高编号到最低编号或相反的行排序,所以这并不是一个问题

i have lines that i want to sort from the highest number to the lowest or the opposite so not really an issue

例如 从这里:

steven:class3 | Pounds: 6
ibesom:class1 | Pounds: 125
heller:class1 | Pounds: 13

对此:

ibesom:class1 | Pounds: 125
heller:class1 | Pounds: 13
steven:class3 | Pounds: 6

谢谢你们

推荐答案

对于简单的排序任务,您可以重新排列行的内容,进行排序,然后将行恢复为原始格式.通常可以使用简单的正则表达式来完成此操作.有关示例,请参见此问题.

For simple sorting tasks you can just rearrange the contents of the line, sort, then return the lines to their original form. This can often be done with simple regular expressions. See this question for an example.

对于更复杂的情况,可能涉及两个或多个项目,可以使用正则表达式从输入行中收集要排序的项目,然后将其插入到行的开头.在排序条件和原始行之间添加分隔符很有用.完成排序后,将删除排序项和分隔符.

For more complex cases, perhaps involving two or more items, the items to be sorted can be collected from the input line using a regular expression and then inserted at the start of the line. It is useful to add a separator between the sort terms and the original line. After the sort is done the sort terms and the separator are removed.

对于这个问题,简单的重新排列然后排序,然后重新排列就足够了.但是,使用更通用的样式很有启发性.

For this question a simple rearrangement then sort and then rearrange back would suffice. But, using the more general style is instructive.

首先,选择一个分隔符.使用要排序的文本中没有出现的字符或字符串.为此,我选择;'#.

Firstly, choose a separator. Use a character or a string that does not occur within the text to be sorted. For this I choose ;'#.

接下来,创建一个正则表达式查找并替换以提取搜索词并构建要排序的行.为此,查找内容为^(.*)\b(\d+)$,替换项为$2 ;'#$0.

Next create a regular expression find and replace to extract the search terms and build the line to be sorted. For this the find-what is ^(.*)\b(\d+)$ and the replace-with is $2 ;'#$0.

这会将示例输入更改为:

This changes the example input into:

6 ;'#steven:class3 | Pounds: 6
125 ;'#ibesom:class1 | Pounds: 125
13 ;'#heller:class1 | Pounds: 13

现在使用菜单=>编辑=>行操作=>排序为升序的整数.产生:

Now use menu => Edit => Line operations => Sort as integers ascending. That yields:

6 ;'#steven:class3 | Pounds: 6
13 ;'#heller:class1 | Pounds: 13
125 ;'#ibesom:class1 | Pounds: 125

使用的方法不限于整数.可以使用任何字符和适当的排序来构建排序项.

The method used is not limited to integers. The sort term can be built using any characters and the appropriate sort used.

最后一步是删除排序项和分隔符.使用正则表达式replace-all查找^.*;'#并不进行任何替换.

The final step is to remove the sort term and the separator. Use a regular expression replace-all finding ^.*;'# and replace with nothing.

唯一的挑战是创建第一个正则表达式以生成排序项并将其添加到行中.总体计划是,查找内容与整个输入行匹配,因此它以^开头,以$结束.然后,替换项具有三个项目排序项",分隔符"和$0.最后的$0包括整个原始输入行.

The only challenge is creating the first regular expression to generate the sort term and add it to the line. The general plan is that the find-what matches the whole of the input line, so it starts with ^ and ends with $. Then the replace-with has the three items "sort term", "separator" and $0. The final $0 includes the whole of the original input line.

这篇关于在记事本中某个字符后对行进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆