doujianjian2060 2014-07-19 04:53
浏览 245
已采纳

正则表达式-解析正则表达式时出错:无效的转义序列:\\ K

I'm trying to compile a regex so that I can extract an 8 digit number with/without spaces between the digits from a string using Go. For some reasons the compilations fails. What should I repalce K with ?

validAcc, err := regexp.Compile(`[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d`)
if err != nil {
    return
}

Play it here

More code with sample data

package main

import "strings"
import "regexp"
import "fmt"

func main() {

    msg := ` 12 34 56 78 //the number we need
 12 3455678 90123455 // the number we don't need`

    acc, err := accFromText(msg)
    if err != nil {
        panic(err)
    }
    exAcc := "12345678"
    if acc != exAcc {
        fmt.Printf("expected %v, received %v", exAcc, acc)
    }

    msg = `
More details here
1234567 12345 123456789 asd
12000000000 a number we don't need 
 12 3456 78 //this is the kind of number we need
 12 3455678 90123455 // the number we don't need`

    acc, err = accFromText(msg)
    if err != nil {
        panic(err)
    }
    exAcc = "12345678"
    if acc != exAcc {
        fmt.Printf("expected %v, received %v", exAcc, acc)
    }

}

func accFromText(msg string) (accNumber string, err error) {
    validAcc, err := regexp.Compile(`[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d`)
    if err != nil {
        return
    }
    accNumber = string(validAcc.Find([]byte(msg)))
    accNumber = strings.Replace(accNumber, " ", "", -1)
    return
}
  • 写回答

3条回答 默认 最新

  • doutang6130 2014-07-19 05:03
    关注

    Considering the go regexp r2 doesn't support any lookbehind/ahead, could you try a simpler expression first:

    c, err := regexp.Compile(`\b\d{8}\b`)
    

    In your case (playground), this would work

    (\d\d ){4}
    validAcc, err := regexp.Compile(`(\d\d ){4}`)
    

    Or:

    (\d\d ?){4} # matches '33 1133 06 Oth'
    validAcc, err := regexp.Compile(`(\d\d ?){4}`)
    

    Again, I try first a simple regexp, before trying more complex option: it will depend on the data you have to parse.


    For a more complex case, the regexp alone can help you capture the data in a group, and then you need to extract the number found (meaning you ned to add post-processing to your regexp):

    validAcc, err := regexp.Compile(`[^\d]((\d\d ?){4})[^\d]`)
    if err != nil {
        return
    }
    accNumber = string(validAcc.Find([]byte(msg)))[1:]
    accNumber = accNumber[:len(accNumber)-1]
    accNumber = strings.Replace(accNumber, " ", "", -1)
    

    See playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。