dongliuxia9495 2019-05-17 02:41
浏览 361
已采纳

Golang中的正则表达式斜线

I have two links:

1: /aaa/bbbb/ccccc.htm
2: /xxx/yyy.htm

What regex is able to match the second link?

I have tried:

^\/.*\/.*[^\/].* 

But, it match all of them.

  • 写回答

1条回答

  • dongshimao7115 2019-05-17 03:33
    关注

    I'm guessing that we might want to pass both URLs, which in that case we would start with:

    (\/[a-z]+)?(?:\.htm)?
    

    We can then add more boundaries, if you wish.

    RegEx

    If this wasn't your desired expression, you can modify/change your expressions in regex101.com.

    enter image description here

    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here

    JavaScript Group Breakup

    const regex = /((\/[a-z]+)?(?:\.htm)?)/gm;
    const str = `/aaa/bbbb/ccccc.htm
    /xxx/yyy.htm`;
    const subst = `Group #1: $1
    Group #2: $2
    `;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);


    If you only wish to pass the second URL and fail the first one, you can simply add some boundaries to your expression, maybe something similar to this would work:

    ^\/[a-z]+\/[a-z]+.htm$
    

    enter image description here

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

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案