【问题标题】:Regex remove everything before the first space occurrence in a line?正则表达式删除一行中第一个空格出现之前的所有内容?
【发布时间】:2018-04-13 11:12:49
【问题描述】:

我想删除每行第一个空格之前的所有字符。

初始文本示例:

  • 2:2 我的狗很好。
  • 1:234 我的猫坏了
  • 14:2 我的青蛙很坏,但它爱我的花园。

结果必须是:

  • 我的狗很好。
  • 我的猫坏了
  • 我的青蛙很坏,但它喜欢我的花园

您会使用什么正则表达式来使用 OpenOffice Calc 或 Notepad++ 实现此结果?

【问题讨论】:

    标签: regex notepad++ openoffice-calc


    【解决方案1】:
    • Ctrl+H
    • 查找内容:^\S+\s+(.+)$
    • 替换为:$1
    • 检查环绕
    • 检查正则表达式
    • 请勿查看. matches newline
    • 全部替换

    说明:

    ^           : beginning of line
      \S+       : 1 or more non space character
      \s+       : 1 or more space character
      (.+)      : group 1, 1 or more any character (ie. rest of the line)
    $           : end of line
    

    替换:

    $1      : content of group 1
    

    给定示例的结果:

    My dog is good.
    My cat is bad
    My frog is bad but it loves my garden.
    

    【讨论】:

      【解决方案2】:
      • Ctrl + h 打开查找和替换对话框
      • 在“查找内容”文本框中写入 ^.*?\s+(.*)$
      • 在“替换为”文本框中写入 $1
      • 检查Regular Expression单选按钮或按ALT+g
      • 您可以点击查找下一个按钮来验证它是否正常工作(查找匹配项)
      • 如果看起来不错,请单击 全部替换 按钮或按 ALT+a

      解释:

      • ^ : 从行首匹配
      • .*?\s+:匹配任何东西,任意次数,直到遇到一个空格(或多个空格)
      • (.*): 捕捉这些空格之后的所有内容
      • $: 匹配到行尾
      • $1:从行访问上面捕获的字符串

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-15
        • 1970-01-01
        • 2016-11-08
        • 2017-10-28
        • 1970-01-01
        • 2010-10-16
        相关资源
        最近更新 更多