I want to extract video id from a vimeo url, can any body help me with the RegEx for the same.
Video url can be of type
https://vimeo.com/173652088
this is just a sample url. RegEx should work for all vimeo urls.
plz try this code
NSString *yourStr = #"https://vimeo.com/173652088";
NSString *str = [yourStr stringByReplacingOccurrencesOfString: #"https://" withString:#""];
NSArray *arryVedios =[str componentsSeparatedByString:#"/"] ;
NSString *id= [ arryVedios lastObject];
NSLog(#"id= %#",id);
this code work for every url.but your id must be in last.
NSString *old = #"https://vimeo.com/173652088";
NSString *new = [old stringByReplacingOccurrencesOfString: #"https://vimeo.com/" withString:#""];
NSLog(#"new= %#", new);
NSString *newone = [[old componentsSeparatedByString:#"/"] lastObject];
NSLog(#"newone= %#", newone);
If you are sure it is a vimeo url then the following would work
[^\/]+(?=\/$|$)
online tester here
online regex tester
Also you can extract the lastComponent from the Url
NSURL* url = [NSURL URLWithString:#"https://vimeo.com/173031270"];
NSLog(#video id: %#", url.lastPathComponent);
Note that this solution will fail if you have a url like "https://vimeo.com/173031270?a=1&b=2"
Related
I dont know why but when I am calling the following URL
it gives me BAD REQUEST - INVALID URL, although this URL is working fine on safari browser and other browsers as well
http://www.ysl.com/wx/shop-product/women/top-handles#{"ytosQuery":"true","department":"handbags_tophandle_w","gender":"D","brand":"","macro":"","micro":"","season":"A,P,E","color":"","size":"","site":"","section":"","sortRule":"","yurirulename":"searchwithdepartment","microcolor":"","agerange":"","macroMarchio":"","page":"2","productsPerPage":"50","modelnames":"","look":"","washtype":"","fabric":"","prints":"","suggestion":"false","suggestionValue":"","material":"","occasion":"","weight":"","gal
I am using following code:
NSString *str = [NSString stringWithFormat:#"%#",[payload stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSString* webStringURL = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webStringURL = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *payload = [NSURL URLWithString:webStringURL];
Any Help Guys? What is that I am doing wrong?
Don't use stringByReplacingPercentEscapesUsingEncoding: on the hole URL, but just on the GET parameters.
Now http:// will also be escaped, thus becoming http%3A%2F%2F which is not valid as an URL.
I would like to extract the id number part from an app store url, for example, following are two app store urls:
https://itunes.apple.com/app/apple-store/id882456583?pt=63826800&ct=%E5%AE%A3%E4%BC%A0%E8%B4%B4001&mt=8
https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo=4
I would like to extract the number after "id", i.e. 882456583 and 639388447.
Anyone knows how to do this? Thank you.
Try this
Swift :
let appStoreUrl = NSURL(string: "https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo=")
let appId = appStoreUrl?.lastPathComponent?.stringByReplacingOccurrencesOfString("id", withString: "")
Objective C :
NSURL *appStoreUrl = [NSURL URLWithString:#"https://itunes.apple.com/app/juan-pi-zhe-kou-shou-ji-shang/id639388447?mt=8&uo="];
NSString *appId = [appStoreUrl.lastPathComponent stringByReplacingOccurrencesOfString:#"id" withString:#""];
Result:
What you are expecting is this
NSURLComponents *components = [NSURLComponents componentsWithURL:[NSURL URLWithString:#"https://itunes.apple.com/app/apple-store/id882456583?pt=63826800&ct=%E5%AE%A3%E4%BC%A0%E8%B4%B4001&mt=8"] resolvingAgainstBaseURL:NO];
NSLog(#"id = %#",[[[components valueForKey:#"path"] componentsSeparatedByString:#"/"] lastObject]);
in logs you ll have required param.
I have a UISearchBar from which I'm extracting the text that represents an address and building from it a JSON URL for Google Geocoder.
NSString* address = searchbar.text;
NSString* url = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?address=%#&sensor=false", address];
If I copy & paste the url from debug window to the browser it works like a charm but when I try to convert it to NSURL i get nil
NSURL* theUrl = [NSURL URLWithString:url];
Any ideas?
I added the encoding to the address prior to concatenating the url and that fixed the problem so now the code looks like this:
NSString* address = searchbar.text;
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, CFBridgingRetain(address), NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8));
NSString* url = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?address=%#&sensor=false", encodedString];
I'm trying to convert a URL to a String with the following code:
NSURL *urlOfOpenedFile = _service.myURLRequest.URL;
NSString *fileThatWasOpened = [NSString stringWithContentsOfURL:urlOfOpenedFile encoding:NSASCIIStringEncoding error:nil];
NSLog(#"url: %#", urlOfOpenedFile);
NSLog(#"string: %#", fileThatWasOpened);
This returns:
url: file://localhost/Users/User/Library/Application%2520Support/iPhone%2520Simulator/6.0/Applications/72AA03A1-3967-4F22-9745-0722C8DE9FAC/Documents/downloads/My%20Document%20.pdf
string: (null)
Can anyone tell me how to convert the URL to a string? Thanks.
You can use
NSString* yourURLString = [urlOfOpenedFile absoluteString];
The method you use is not for converting a NSURL to a NSString. Instead like the method name it returns the string from content of that URL. Since you have PDF file (not string) on that URL it returns nil.
For converting you can use the following instance method of NSURL
- (NSString *)absoluteString;
you can use like
NSString *fileThatWasOpened = [NSString stringWithFormat:#"%#", url];
I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath.
Now in the case of the built-in app images I do get the file name as an NSString an save in the [occasion imagePath]. But in the 2nd case where the user picks an image form the photo library I get an NSURL which I want to convert to an NSString to be able to save it in [occasion imagePath].
Is it possible to convert the NSURL to an NSString?
In Objective-C:
NSString *myString = myURL.absoluteString;
In Swift:
var myString = myURL.absoluteString
More info in the docs:
If you're interested in the pure string:
[myUrl absoluteString];
If you're interested in the path represented by the URL (and to be used with NSFileManager methods for example):
[myUrl path];
Try this in Swift :
var urlString = myUrl.absoluteString
Objective-C:
NSString *urlString = [myURL absoluteString];
Swift update:
var myUrlStr : String = myUrl.absoluteString
I just fought with this very thing and this update didn't work.
This eventually did in Swift:
let myUrlStr : String = myUrl!.relativePath!
You can use any one way
NSString *string=[NSString stringWithFormat:#"%#",url1];
or
NSString *str=[url1 absoluteString];
NSLog(#"string :: %#",string);
string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif
NSLog(#"str :: %#", str);
str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/RemoDuplicateMedia.app/loading_circle_animation.gif
In Swift :-
var str_url = yourUrl.absoluteString
It will result a url in string.