golang regexp删除所有空白行 [英] golang regexp remove all blank lines

查看:83
本文介绍了golang regexp删除所有空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用golangs regexp替换所有空白行和仅用空格/制表符填充的行.我认为以下正则表达式应该可以解决问题,emptyLINE:= regexp.MustCompile(`^ \ s * $`)但令人惊讶的是,行 ^ 的开始和行 $ 的行的正则表达式标记不起作用.它们似乎表示整个字符串的开始/结束,而不仅仅是字符串中的一行,请参见

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

我在这里想念东西吗?

维克托人的回答几乎使我到了那里,但我仍然无法删除所有需要的行: https://play.golang.org/p/1IpETpFKCU

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

推荐答案

您需要传递(?m)内联修饰符:

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

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

MULTILINE修饰符将使 ^ 与以下内容的开头匹配 line $ 将与 line 的结尾匹配:

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

m        多行模式: ^ $ 匹配除开始/结束文本外,还包含开始/结束行(默认为false)

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

要记住的另一件事是 \ s [\ t \ n \ f \ r] 符号匹配.如果要匹配所有水平空格,则可以使用 [\ t] [\ t \ p {Zs}] .这样一来,您就可以保持在界线之内.

Another thing to bear in mind is that \s matches [\t\n\f\r ] 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.

还有另一件事: $ 仅在换行后才声明位置,它不消耗它,因此,您需要实际匹配 \ r \ n $ 之后的 \ r \ n (如果您也需要删除换行符).

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

这就是我想出的(演示):

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`(?m)^\s*$[\r\n]*|[\r\n]+\s+\z`)
    in := ` 
      test 


    test  
     `
    want_empty := `   test 
    test    `
    fmt.Printf("have [%v]\n", in)
    fmt.Printf("want [%v]\n", want_empty)
    fmt.Printf("got  [%v]\n", re.ReplaceAllString(in, ""))
}

^ \ s * $ [\ r \ n] * -匹配行的开头,任意0+空格,行尾的资产( $ ),然后匹配0+ LF/CR符号.

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

[\ r \ n] + \ s + \ z 替代匹配1个或多个CR或LF符号,1 +个空格,然后匹配字符串 \ z ,如果没有它, ^ \ s * $ [\ r \ n] * 将不匹配最后一个空行.

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

这篇关于golang regexp删除所有空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆