doubeng9407 2016-10-13 14:48
浏览 337
已采纳

golang regexp删除所有空白行

I want to replace all blank lines and lines filled only with spaces/tabs using golangs regexp. I thought the following regexp should do the trick, emptyLINE := regexp.MustCompile(`^\s*$`) but was surprised, that the begin of line ^ and end of line $ regexp tags do not work. They rather seem to signify the start/end of the whole string instead of just a line within the string, see

https://play.golang.org/p/WZ4flVtDMN

Am I missing something here?

EDIT:

Wiktors answer almost got me there, still I cannot remove all wanted lines: https://play.golang.org/p/1IpETpFKCU

  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2016-10-13 14:49
    关注

    You need to pass the (?m) inline modifier:

    regexp.MustCompile(`(?m)^\s*$`) 
                        ^^^^
    

    The MULTILINE modifier will make ^ match the start of the line and $ will match the end of a line:

    m        multi-line mode: ^ and $ match begin/end line in addition to begin/end text (default false)

    Another thing to bear in mind is that \s matches [\t \f ] symbols. If you want to match all horizontal whitespaces, you may use [ \t] or [\t\p{Zs}]. That will let you stay inside the line bounds.

    And another thing: $ only asserts the position after a line break, it does not consume it, so, you need to actually match or or after $ (if you need to remove the linebreaks, too).

    This is what I came up with (demo):

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        re := regexp.MustCompile(`(?m)^\s*$[
    ]*|[
    ]+\s+\z`)
        in := ` 
          test 
    
    
        test  
         `
        want_empty := `   test 
        test    `
        fmt.Printf("have [%v]
    ", in)
        fmt.Printf("want [%v]
    ", want_empty)
        fmt.Printf("got  [%v]
    ", re.ReplaceAllString(in, ""))
    }
    

    The ^\s*$[ ]* - matches the start of a line, any 0+ whitespaces, assets the end of a line ($) and then matches 0+ LF/CR symbols.

    The [ ]+\s+\z alternative matches 1 or more CR or LF symbols, 1+ whitespaces and then the unambiguous end of string \z, without it, ^\s*$[ ]* won't match the last empty line.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
  • ¥15 这怎么做,怎么在我的思路下改下我这写的不对
  • ¥50 数据库开发问题求解答
  • ¥15 安装anaconda时报错
  • ¥20 如何用Python处理单元格内连续出现的重复词语?
  • ¥15 小程序有个导出到插件方式,我是在分包下引入的插件,这个export的路径对吗,我看官方文档上写的是相对路径
  • ¥20 希望有人能帮我完成这个设计( *ˊᵕˋ)
  • ¥100 将Intptr传入SetHdevmode()将Intptr传入后转换为DEVMODE的值与外部代码不一致
  • ¥50 基于ERA5数据计算VPD
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部