【问题标题】:Notepad ++ group substitutions using \k使用 \k 的记事本 ++ 组替换
【发布时间】:2020-01-21 00:30:37
【问题描述】:

如何在 Notepad++ 中使用正则表达式将属于一组的所有项目放在一行上?

我有一组练习题答案,格式如下:

29  Continuous
29  Horizontal mattress
29  Interrupted
29  Vertical mattress
29  No use sutures because the immediate denture acts as a stint
30  1-5 years
30  6-10 years
30  11-15 years
30  16-20 years
30  21-25 years
31  Artery
32  Vitality of the tooth pulp
32  Age of the patient
32  Absence of root resorption

我希望对结果进行格式化,使每组都在一行上:

29 Continuous Horizontal mattress Vertical mattress No use sutures because the immediate denture acts as a stint
30 1-5 years 6-10 years 11-15 years 16-20 years 21-25 years
31 Artery
32 Vitality of the tooth pulp Age of the patient Absence of root resorption

我编写了以下正则表达式来选择集合:

^(?'number'^\d{0,4})\t(.*$)(\n\k<number>(.*))*

\1 \2 \4 代替

29 Continuous   No use sutures because the immediate denture acts as a stint
30 1-5 years    21-25 years
31 Artery 

我在这里做错了什么?

【问题讨论】:

  • 当您使用应用于组的量词时,仅保留最后一个匹配项。这就是您与可能出现 0 次或多次的第四组一起使用的内容,
  • 我在您的正则表达式中看到的一个问题是,您没有在示例文本中使用制表符,而是使用两个空格,在修复此问题后,您的正则表达式匹配,但是您尝试做的事情是不可能的正则表达式。您需要使用 Python 等脚本语言进行一些编程。

标签: regex notepad++ regex-group


【解决方案1】:

我想出了一个解决方案,以防有人尝试做类似的事情。

选择集合

(^(?'number'^\d{0,4})\t(.*$)(\n\k<number>(.*))*)

原来贴出的代码的区别在于整个代码周围的()

替换为\1\n 以创建以下格式的集合:

29  Continuous
29  Horizontal mattress
29  Interrupted
29  Vertical mattress
29  No use sutures because the immediate denture acts as a stint

30  1-5 years
30  6-10 years
30  11-15 years
30  16-20 years
30  21-25 years

31  Artery

32  Vitality of the tooth pulp
32  Age of the patient
32  Absence of root resorption

选择单独的行
(^(?'number'^\d{0,4})\t(.*$))\n

替换成\3(\3后面两个空格)来创建

Continuous  Horizontal mattress  Interrupted  Vertical mattress  No use sutures because the immediate denture acts as a stint   
1-5 years  6-10 years  11-15 years  16-20 years  21-25 years    
Artery  
Vitality of the tooth pulp  Age of the patient  Absence of root resorption

我不知道如何使用正则表达式添加问题#s,但如果需要,它可以很容易地导入到 excel 中。

【讨论】:

    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2016-11-02
    • 2015-10-15
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多