Regular Expression to collect everything after the last /

前端 未结 8 1827
北荒
北荒 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:43

    This matches at least one of (anything not a slash) followed by end of the string:

    [^/]+$
    


    Notes:

    • No parens because it doesn't need any groups - result goes into group 0 (the match itself).
    • Uses + (instead of *) so that if the last character is a slash it fails to match (rather than matching empty string).


    But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' ) or equivalent function.

    For PHP, the closest function is strrchr which works like this:

    strrchr( Text , '/' )
    

    This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:

    substr( strrchr( Text, '/' ), 1 );
    

提交回复
热议问题