正则表达式在Go中不起作用 [英] Regular expression doesn't work in Go

查看:67
本文介绍了正则表达式在Go中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我是一名正则表达式的业余爱好者,但是我对于为什么这样的代码段在Go中不起作用感到非常困惑

forgive me for being a regex amateur but I'm really confused as to why this doesn't piece of code doesn't work in Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    var a string = "parameter=0xFF"
    var regex string = "^.+=\b0x[A-F][A-F]\b$"
    result,err := regexp.MatchString(regex, a)
    fmt.Println(result, err)
}
// output: false <nil>

这似乎可以在python中正常工作

This seems to work OK in python

import re

p = re.compile(r"^.+=\b0x[A-F][A-F]\b$")
m = p.match("parameter=0xFF")
if m is not None:
    print m.group()

// output: parameter=0xFF

我要做的就是匹配输入的格式是否为< anything> = 0x [A-F] [A-F]

All I want to do is match whether the input is in the format <anything>=0x[A-F][A-F]

任何帮助将不胜感激

推荐答案

您是否尝试过使用原始字符串文字(用反引号代替引号)?像这样:

Have you tried using raw string literal (with back quote instead of quote)? Like this:

var regex string = `^.+=\b0x[A-F][A-F]\b$`

这篇关于正则表达式在Go中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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