2016-10-04 10:48 阅读 257


Hi I have the following text:


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 duanlianyun0462 2016-10-04 10:55

    You may try regex expression below


    A working example is here:


    [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;

    点赞 评论 复制链接分享
  • doufei3561 doufei3561 2016-10-04 10:54

    Without regex you can do:

    $url_parts = parse_url('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/');
    echo end(explode('/', $url_parts['path']));

    or better:

    $url_path = parse_url('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/', PHP_URL_PATH);
    echo end(explode('/', $url_path));
    点赞 评论 复制链接分享
  • douza19870617 douza19870617 2016-10-04 10:57

    basename() also works, so you can also do like this:

    echo basename('file:/home/dx/reader/validation-garage/IDON/test-test-test@2016-10-04.txt#/');
    点赞 评论 复制链接分享