doujiang2020 2018-01-16 14:27
浏览 128
已采纳

将已编译的正则表达式转换为字符串

I don't have much experience in Go, but basically I want to print my regex on the screen after using it. I cant find anything on Google. This seems something rather easy to do but I have tried several things and nothing else worked.

var swagger_regex = regexp.MustCompile(`[0-9][.][0-9]`)
.... some code here ....
fmt.Println("Your '_.swagger' attribute does not match " + string(swagger_regex))
  • 写回答

1条回答 默认 最新

  • dongliang2058 2018-01-16 14:30
    关注

    The regexp.Regexp type has a Regexp.String() method which does this exactly:

    String returns the source text used to compile the regular expression.

    You don't even have to call it manually, as the fmt package checks and calls the String() method if the type of the passed value has it.

    Example:

    r := regexp.MustCompile(`[0-9][.][0-9]`)
    fmt.Println("Regexp:", r)
    
    // If you need the source text as a string:
    s := r.String()
    fmt.Println("Regexp:", s)
    

    Output (try it on the Go Playground):

    Regexp: [0-9][.][0-9]
    Regexp: [0-9][.][0-9]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题