Regular Expression to collect everything after the last /

前端 未结 8 1669
北荒
北荒 2020-11-28 05:16

I\'m new at regular expressions and wonder how to phrase one that collects everything after the last /.

I\'m extracting an ID used by Google\'s GData.

相关标签:
8条回答
  • 2020-11-28 05:58

    Generally:

    /([^/]*)$
    

    The data you want would then be the match of the first group.


    Edit   Since you’re using PHP, you could also use strrchr that’s returning everything from the last occurence of a character in a string up to the end. Or you could use a combination of strrpos and substr, first find the position of the last occurence and then get the substring from that position up to the end. Or explode and array_pop, split the string at the / and get just the last part.

    0 讨论(0)
  • 2020-11-28 05:58

    This pattern will not capture the last slash in $0, and it won't match anything if there's no characters after the last slash.

    /(?<=\/)([^\/]+)$/
    

    Edit: but it requires lookbehind, not supported by ECMAScript (Javascript, Actionscript), Ruby or a few other flavors. If you are using one of those flavors, you can use:

    /\/([^\/]+)$/
    

    But it will capture the last slash in $0.

    0 讨论(0)
提交回复
热议问题