dprfe04886 2015-12-04 16:09
浏览 16
已采纳

改善正则表达

I want to match something like this in PHP:

class-11/xxx/xxx/xx/xxx/things_to_remember/
class-12/xxx/xxx/xx/xxx/things_to_remember/

However I don't want to match something like this:

xxx/class-11/xxx/
class-11/xxx/things_to_remember/xxx
class-11/xxx/

I am writing it like this:

^(class-[12]{2})/.+/things_to_remember/$

I heard regular expression have many features like greedy etc. and they also need to be efficient ? Is the above regualar expression good ?

  • 写回答

1条回答 默认 最新

  • dosrmo0442 2015-12-04 16:31
    关注

    I wrote a little regex here that captures it like the format you wrote. It might need some slight changes as I didn't know how many digits could be in after class.

    /
    class-(\d{2}) #Matches class, makes sure that class only is 2 digits - captures class digits
    \/([^\/]{3}) #captures first 3 characters that aren't a slash.
    \/([^\/]{3}) #Capture 3 characters again.
    \/([^\/]{2}) #captures two characters
    \/([^\/]{3}) #Captures 3 characters
    \/things_to_remember\/ #Matches last piece of string.
    /xg
    

    You can test it out here.

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题