从url字符串中获取最后一个'/'(斜杠)之后的所有字符

fel*_*wcf 6 regex substring objective-c filter ios

如何从url字符串中获取最后一个'/'(斜杠)之后的所有字符?

在为某个站点发出webview请求时,我从URL方案中收到一个json字符串.例如:

应用:// GA /电子商务/%7B%22product%22:%22playstation4%22 ...}]

我想在最后一个 '/'(斜杠)之后抓取子串.

我怎样才能实现它?我可以知道正则表达式的格式是什么?

避免使用

NSRange lastDotRange = [sURL rangeOfString:@"/" options:NSBackwardsSearch];
Run Code Online (Sandbox Code Playgroud)

因为我可能在我的json中逃脱了'/'.

谢谢.

9Ba*_*nap 15

对于Swift 3试试这个.

let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/")
let finalFileName = fileArray?.last
Run Code Online (Sandbox Code Playgroud)

输出:"这里"


win*_*ktw 5

尝试这个:

    NSURL *url = [NSURL URLWithString:@"app://ga/ecommerce/%7B%22product%22:%22playstation4%22..."];
    NSString *last = [url lastPathComponent];
Run Code Online (Sandbox Code Playgroud)