Notepadd++ Python脚本:如果行以X开头,则删除最后3个“ – ”之间的所有内容。

huangapple go评论98阅读模式
英文:

Notepadd++ Python Script: Delete everything between the last 3 " - " if line starts with X

问题

在查看行末尾逆向计数“ - ”(注意空格)并删除文本,但仅当行以fx TEST开头时。

之前

  1. TESTMouse - Dog - CAT - Fish
  2. Mouse - Dog - CAT - Fish
  3. TESTRat - Whale - Bird - Horse - Snake

之后

  1. TESTMouse
  2. Mouse - Dog - CAT - Fish
  3. TESTRat - Whale

这已经超出了我自己的能力,我在搜索中也没有找到任何可行的解决方法。

英文:

Looking at the end of the line counting the " - " backwards (notice the spaces) and delete the text but only if the line starts with fx TEST.

Before

  1. TESTMouse - Dog - CAT - Fish
  2. Mouse - Dog - CAT - Fish
  3. TESTRat - Whale - Bird - Horse - Snake

After

  1. TESTMouse
  2. Mouse - Dog - CAT - Fish
  3. TESTRat - Whale

This is beyond my ability to write myself and I have not found anything that would work in my searches.

答案1

得分: 0

以下是使用Python的字符串函数编写的非常简单的答案,可能可以使用正则表达式来缩短答案。请测试它并告诉我是否有任何问题。

  1. with open("testfile.txt", "r") as f:
  2. lines = f.readlines()
  3. with open("testfile.txt", "w") as f:
  4. for line in lines:
  5. # 去除末尾的换行符
  6. line = line.rstrip('\n')
  7. if line.startswith("TEST"):
  8. # 将行拆分为以 ' - ' 为分隔符的部分
  9. parts = line.split(' - ')
  10. # 保留前3个部分
  11. if len(parts) > 3:
  12. line = ' - '.join(parts[:-3])
  13. f.write(line + '\n')

请注意,代码部分未进行翻译,只返回翻译好的内容。

英文:

Here is a very simple answer using Pythons string functions, probably could get a bit shorter of an answer with regex. Please test it and tell me if you have any issues.

  1. with open("testfile.txt", "r") as f:
  2. lines = f.readlines()
  3. with open("testfile.txt", "w") as f:
  4. for line in lines:
  5. # strip trailing newline
  6. line = line.rstrip('\n')
  7. if line.startswith("TEST"):
  8. # split line into parts from ' - '
  9. parts = line.split(' - ')
  10. # keep first 3 parts
  11. if len(parts) > 3:
  12. line = ' - '.join(parts[:-3])
  13. f.write(line + '\n')

答案2

得分: 0

不需要使用Notepad++ PythonScript,简单的查找和替换就足够了:

  • Ctrl+H
  • 查找内容:^TEST.*?\K(?: - \w+){3}$
  • 替换为:LEAVE EMPTY
  • 勾选 区分大小写
  • 勾选 环绕搜索
  • 选择 正则表达式
  • 取消勾选 匹配换行符
  • 全部替换

解释:

  1. ^ # 行首
  2. TEST # 字面上的 TEST
  3. .*? # 0 或多个任意字符,非贪婪模式
  4. \K # 忘记我们到这个位置为止所看到的所有内容
  5. (?: # 非捕获组
  6. - \w+ # 空格、连字符、空格,1 个或多个单词字符
  7. ){3} # 结束组,必须出现 3 次
  8. $ # 行尾

替换:

屏幕截图(之前):

Notepadd++ Python脚本:如果行以X开头,则删除最后3个“ – ”之间的所有内容。

屏幕截图(之后):

Notepadd++ Python脚本:如果行以X开头,则删除最后3个“ – ”之间的所有内容。

英文:

No needs for Notepad++ PythonScript, a simple find and replace is enough:

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: ^TEST.*?\K(?: - \w+){3}$
  • Replace with: LEAVE EMPTY
  • TICK Match case
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • <kbd>Replace all</kbd>

Explanation:

  1. ^ # beginning of line
  2. TEST # literally
  3. .*? # 0 or more any character, not greedy
  4. \K # forget all we have seen until this position
  5. (?: # non capture group
  6. - \w+ # space, hyphen, space, 1 or more word character
  7. ){3} # end group, must appear 3 times
  8. $ # end of line

Replacement:

Screenshot (before):

Notepadd++ Python脚本:如果行以X开头,则删除最后3个“ – ”之间的所有内容。

Screenshot (after):

Notepadd++ Python脚本:如果行以X开头,则删除最后3个“ – ”之间的所有内容。

huangapple
  • 本文由 发表于 2023年6月19日 17:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定