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

时间:2022-10-20 09:59:53

How to get all characters after the last '/' (slash) from url string?

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

I receive a json string from my URL scheme when making webview request for certain site. For example:

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

app://ga/ecommerce/%7B%22product%22:%22playstation4%22...}]

I would like to grab the substring after the last '/'(slash).

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

How can I achieve it? May I know what is the format of Regex?

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

Avoid to use

避免使用

NSRange lastDotRange = [sURL rangeOfString:@"/" options:NSBackwardsSearch];

because I might have escaped '/' in my json.

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

Thank you.

6 个解决方案

#1


7  

For Swift 3 try this.

对于Swift 3试试这个。

let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/"
let finalFileName = fileArray?.last

Output : "Here"

输出:“这里”

#2


2  

You may split string in array and get last object from array like following:

您可以在数组中拆分字符串并从数组中获取最后一个对象,如下所示

NSString *myString = @"app://ga/ecommerce/product:playstation4"; 
NSArray* spliteArray = [myString componentsSeparatedByString: @"/"];
NSString* lastString = [spliteArray lastObject];

#3


1  

Create an NSURL and use the NSURL methods, like lastPathComponent, or parameterString. These methods are presumably written by someone who knows how to handle URLs.

创建NSURL并使用NSURL方法,如lastPathComponent或parameterString。这些方法可能是由知道如何处理URL的人编写的。

#4


1  

try this:

    NSURL *url = [NSURL URLWithString:@"app://ga/ecommerce/%7B%22product%22:%22playstation4%22..."];
    NSString *last = [url lastPathComponent];

#5


0  

The regex you need is something like this:

你需要的正则表达式是这样的:

((\/){1}([a-z\-]*))$

Which will match 1 slash, with all lowercase letters and hyphens. You can add more characters in there, like A-Z for capital letters (etc), and the '$' places matches it from the end of the string,

哪个匹配1斜杠,全部小写字母和连字符。您可以在其中添加更多字符,例如A-Z表示大写字母(等),'$'表示从字符串末尾匹配它,

#6


0  

Well it seems like using Regex is quite expensive inside shouldStartLoadWithRequest delegate especially if you're having hybrid app which has plenty webviews. Some of my websites in webview might have more than one request, some are running at the background. It's painful if the webview triggers regex codes everytime when webview loads the request.

好吧,似乎使用正则表达式在shouldStartLoadWithRequest委托中非常昂贵,特别是如果你有拥有大量webview的混合应用程序。我在webview中的一些网站可能有多个请求,有些是在后台运行。如果webview在每次webview加载请求时触发正则表达式代码,那将会很痛苦。

And also, I have escaped '/' in my last component params (json string), it might cause the lastComponent breaks after the escaped '/' character.

而且,我已经在我的最后一个组件params(json字符串)中转义'/',它可能导致lastComponent在转义'/'字符后中断。

Therefore I stick to the codes filtering by using if-else statements and also compare strings with components of URL For example

因此,我坚持使用if-else语句进行代码过滤,并将字符串与URL的组件进行比较。例如

request.URL.absoluteString
request.URL.host
request.URL.fragment

and also found out that @Nitin Gohel is useful.

并且还发现@Nitin Gohel很有用。

#1


7  

For Swift 3 try this.

对于Swift 3试试这个。

let fileName = "your/file/name/here"
let fileArray = fileName?.components(separatedBy: "/"
let finalFileName = fileArray?.last

Output : "Here"

输出:“这里”

#2


2  

You may split string in array and get last object from array like following:

您可以在数组中拆分字符串并从数组中获取最后一个对象,如下所示

NSString *myString = @"app://ga/ecommerce/product:playstation4"; 
NSArray* spliteArray = [myString componentsSeparatedByString: @"/"];
NSString* lastString = [spliteArray lastObject];

#3


1  

Create an NSURL and use the NSURL methods, like lastPathComponent, or parameterString. These methods are presumably written by someone who knows how to handle URLs.

创建NSURL并使用NSURL方法,如lastPathComponent或parameterString。这些方法可能是由知道如何处理URL的人编写的。

#4


1  

try this:

    NSURL *url = [NSURL URLWithString:@"app://ga/ecommerce/%7B%22product%22:%22playstation4%22..."];
    NSString *last = [url lastPathComponent];

#5


0  

The regex you need is something like this:

你需要的正则表达式是这样的:

((\/){1}([a-z\-]*))$

Which will match 1 slash, with all lowercase letters and hyphens. You can add more characters in there, like A-Z for capital letters (etc), and the '$' places matches it from the end of the string,

哪个匹配1斜杠,全部小写字母和连字符。您可以在其中添加更多字符,例如A-Z表示大写字母(等),'$'表示从字符串末尾匹配它,

#6


0  

Well it seems like using Regex is quite expensive inside shouldStartLoadWithRequest delegate especially if you're having hybrid app which has plenty webviews. Some of my websites in webview might have more than one request, some are running at the background. It's painful if the webview triggers regex codes everytime when webview loads the request.

好吧,似乎使用正则表达式在shouldStartLoadWithRequest委托中非常昂贵,特别是如果你有拥有大量webview的混合应用程序。我在webview中的一些网站可能有多个请求,有些是在后台运行。如果webview在每次webview加载请求时触发正则表达式代码,那将会很痛苦。

And also, I have escaped '/' in my last component params (json string), it might cause the lastComponent breaks after the escaped '/' character.

而且,我已经在我的最后一个组件params(json字符串)中转义'/',它可能导致lastComponent在转义'/'字符后中断。

Therefore I stick to the codes filtering by using if-else statements and also compare strings with components of URL For example

因此,我坚持使用if-else语句进行代码过滤,并将字符串与URL的组件进行比较。例如

request.URL.absoluteString
request.URL.host
request.URL.fragment

and also found out that @Nitin Gohel is useful.

并且还发现@Nitin Gohel很有用。