duanlu9557 2017-02-13 12:20
浏览 32
已采纳

如何从前瞻转换正则表达式

I have the following Regex in PHP and other code that is doing a great job.

/^(?:(?=[^ ]+\d)(?:[A-Z0-9]+))|(?:[A-Z0-9]+) +?(?=.*\d)(?:[A-Z0-9]+)?

Regular expression visualization

Debuggex Demo

It turns out that Go doesn't support lookheads and I'm at a loss on how to convert it across.

The Debuggex link has some test data I was using to qualify the code before.

  • 写回答

1条回答 默认 最新

  • doufei5315 2017-02-13 15:55
    关注

    Firstly, the left hand side of the alternation /^... matches nothing, because / can never appear before start of input.

    That leaves just (?:[A-Z0-9]+) +?(?=.*\d), which can be expressed as:

    ((?:[A-Z0-9]+) ).*\d.*
    

    The term " +?" as used is identical in effect to just " ".

    Note however that this consumes more input than the original, which is unavoidable. Your original match is now in group 1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用