Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用Go删除yaml文件中与正则表达式匹配的行_Regex_Go - Fatal编程技术网

Regex 使用Go删除yaml文件中与正则表达式匹配的行

Regex 使用Go删除yaml文件中与正则表达式匹配的行,regex,go,Regex,Go,我有以下yamlmyfile.yml文件: variables: PYTHONUNBUFFERED: 1 GIT_STRATEGY: clone Grpc__Client__Service__Target: ${URL}.${PREFIX}.mydomain.com:443 include: - project: "myproject" file: "/test.yml" stages: - mystage 我想删除以“Grpc”开头的行 我的代码: packa

我有以下yaml
myfile.yml
文件:

variables:
  PYTHONUNBUFFERED: 1
  GIT_STRATEGY: clone
  Grpc__Client__Service__Target: ${URL}.${PREFIX}.mydomain.com:443

include:
  - project: "myproject"
    file: "/test.yml"

stages:
  - mystage
我想删除以“Grpc”开头的行

我的代码:

package main

import (
    "fmt"
    "io/ioutil"
    "strings"
    "regexp"
)

func main() {

    filename := "myfile.yml"

    content, err := ioutil.ReadFile(filename)
    check(errReadFile)

    if err != nil {
      panic(err)
    }

    m := regexp.MustCompile("^  Grpc__(.*)$") 
    grpcRemoved := m.ReplaceAllString(string(content), "")
    fmt.Println(grpcRemoved)
}
最后,没有任何东西被移除

我使用
MatchString
测试了我的正则表达式,它返回了
true

matchString, _ := regexp.MatchString("^  Grpc__(.*)$", "  Grpc__Client__Service__Target: ${URL}.${PREFIX}.mydomain.com:443")
但是

返回的
false
,这不是我所期望的。然后我假设
ReplaceAllString
不起作用,因为它找不到任何可替换的内容


我的代码怎么了?

这是因为您的正则表达式:
“^Grpc_uuuuu(.*)”
^
表示字符串的开头,
$
表示字符串的结尾

此字符串:
“Grpc\uuuuu客户端\uuuu服务\uuuu目标:${URL}.${PREFIX}.mydomain.com:443”
匹配,因为它以
Grpc\uuuu
开头,以
mydomain.com:443
结尾

但是yaml文件中的字符串以
变量开始:…
并以
结束-mystage
因此不匹配


尝试使用
“Grpc_uu(.*)”
(无
^
$
),它会工作。

或启用多行模式:请参阅
matchStringFile, _ := regexp.MatchString("^  Grpc__(.*)$", string(content))