dt2015 2015-02-28 18:52
浏览 46
已采纳

负向后看失败

I have a little MySQL table inspector script in my Golang code. The last thing I wanted to do was to store any possible enum value when my application starts so I can read it out of the buffer instead of having to describe the tables constantly for API calls.

Now with enum's stuff like 'value1','value2','value\'s3 can occur, where simple RegEx like '(.*?)' doesn't work because of the escaped quotation-mark in value\'s3. So I wrote a negative look-behind to make it match only if it is a non-escaped quotation-mark. Not totally flawless but at least some extra checks and I manage the database structure by myself so it's something I can just keep a small note on.

To the point; the negative-lookbehind fails '(.*?)(?<!\\)':

panic: regexp: Compile(`'(.*?)(?<!\\)'`): error parsing regexp: invalid or unsupported Perl syntax: `(?<`

Code:

var enumMatchValues = regexp.MustCompile(`'(.*?)(?<!\\)'`)
values := enumMatchValues.FindAllStringSubmatch(theRawEnumValuesString, -1)

Are the lookaheads/lookbehinds officially not part of the core of the RegEx functionality or is this just something Golang hasn't implemented yet?

Would there be another workaround or extended RegEx package for Go to solve this?

  • 写回答

1条回答 默认 最新

  • dqfr90979 2015-02-28 19:05
    关注

    Lookarounds are not supported, you could use a possible workaround:

    re := regexp.MustCompile(`'(.*?[^\\]|)'`)
    values := re.FindAllStringSubmatch(theRawEnumValuesString, -1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案