【问题标题】:Notepad++: Delete everything after a number of characters in stringNotepad ++:删除字符串中多个字符后的所有内容
【发布时间】:2015-01-24 10:31:39
【问题描述】:

以下是 Notepadd ++ 中每行 24 个字符的示例。我需要将每行的字符限制为 14 个字符。

见鬼,她今天怎么样?

我需要它看起来像下面这样:

哎呀,怎么样

我使用了这个代码

Find what: ^(.{1,14}).*
Replace with: $1

但是,它显示“Hell, how is s”,拼写错误。

如何在 Notepad++ 中将字符数限制为每行 14 个字符并删除最后一个单词?

【问题讨论】:

  • Hell, how is s 有 14 个字符。
  • 尝试^(.{14}).*并将匹配替换为$1
  • 我认为 OP 意味着该行应该最多 14个字符,但如果它将一个单词切成两半,则应该进一步截断。因此,与其保留“she”的“s”,不如删除整个单词。

标签: regex notepad++ limit


【解决方案1】:

这应该适合你:

查找内容:^(.{1,14}(?<=\S)\b).*$

替换为:$1

所以对于Hell, how is she today ?,输出为:Hell, how is

DEMO

^                # The beginning of the string
(                # Group and capture to \1:
  .{1,14}        # Any character except \n (between 1 and 14 times (matching the most amount possible))
  (?<=\S)        # This lookbehind makes sure the last char is not a space
  \b             # The boundary between a word char (\w). It matches the end of a word in this case
)                # End of \1
.*$              # Match any char till the end of the line

【讨论】:

    【解决方案2】:

    这是你想要的吗:

    查找内容:^(.{1,14}) .*$
    替换为:$1

    如果有空格,这将截断为 14 个字符或更少。

    【讨论】:

    • 考虑多个空格的情况
    【解决方案3】:

    也可以使用\K 作为可变长度的后视并替换为空:

    ^.{0,13}\w\b\K.*
    

    \w 匹配 word character\bword boundary

    Test at regex101.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多