douliao5942 2016-10-04 10:48
浏览 812
已采纳

正则表达式在两个特定正斜杠之间提取字符串

Hi I have the following text:

file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/

I need to retrieve test-test-test@2016-10-04.txt# from the string above. If I can also exclude the hash even better.

I've tried looking at examples like this Regex to find text between second and third slashes but having trouble getting it working, can anyone help?

I'm using PHP regex to do this.

  • 写回答

3条回答 默认 最新

  • duanlianyun0462 2016-10-04 10:55
    关注

    You may try regex expression below

    \/([a-z\-]*\@[0-9\-\.]*[a-z]{3}\#)\/
    

    A working example is here: https://www.regex101.com/r/RYsh7H/1

    Explanation:

    [a-z\-]* => Matches test-test-test part with lowercase and can contain dahses
    \@ => Matches constant @ sign
    [0-9\-\.]* => Matches the file name with digits, dashes and {dot}
    [a-z]{3}\# => Matches your 3 letter extension and #
    

    PS: If you really do not need # you do not have to use regex. And you may consider using parse_url method of PHP.

    Hope this helps;

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

报告相同问题?

悬赏问题

  • ¥15 YOLOv5在进行trainpy训练后为什么会出现这种情况(语言-python)
  • ¥15 关于远程桌面的鼠标位置转换
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!