nsregularexpression

Check if string contains special characters in Swift

限于喜欢 提交于 2019-11-27 09:53:10
问题 I have to detect whether a string contains any special characters. How can I check it? Does Swift support regular expressions? var characterSet:NSCharacterSet = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789") if (searchTerm!.rangeOfCharacterFromSet(characterSet).location == NSNotFound){ println("Could not handle special characters") } I tried the code above, but it matches only if I enter the first character as a special character. 回答1:

How to write regular expressions in Objective C (NSRegularExpression)?

五迷三道 提交于 2019-11-27 06:27:23
I have this regex working when I test it in PHP but it doesn't work in Objective C: (?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?) I tried escaping the escape characters but that doesn't help either. Should I escape any other character? This is my code in Objective C: NSMutableString *searchedString = [NSMutableString stringWithString:@"domain-name.tld.tld2"]; NSError* error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"

Using NSRegularExpression to extract URLs on the iPhone

倖福魔咒の 提交于 2019-11-27 04:39:47
I'm using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I'm only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn't returning NSStrings for each URL, but the objects descriptions only. How do I make my arrayOfAllMatches return all URLs, as NSStrings? -(NSArray *)stripOutHttp:(NSString *)httpLine { // Setup an NSError object to catch any failures NSError *error = NULL; // create the NSRegularExpression object and initialize it with a pattern // the pattern will match any http or https url, with

Capture groups not working in NSRegularExpression

懵懂的女人 提交于 2019-11-27 00:47:41
问题 Why is this code only spitting out the entire regex match instead of the capture group? Input @"A long string containing Name:</td><td>A name here</td> amongst other things" Output expected A name here Actual output Name:</td><td>A name here</td> Code NSString *htmlString = @"A long string containing Name:</td><td>A name here</td> amongst other things"; NSRegularExpression *nameExpression = [NSRegularExpression regularExpressionWithPattern:@"Name:</td>.*\">(.*)</td>" options

Using NSRegularExpression to extract URLs on the iPhone

和自甴很熟 提交于 2019-11-26 12:45:37
问题 I\'m using the following code on my iPhone app, taken from here to extract all URLs from striped .html code. I\'m only being able to extract the first URL, but I need an array containing all URLs. My NSArray isn\'t returning NSStrings for each URL, but the objects descriptions only. How do I make my arrayOfAllMatches return all URLs, as NSStrings? -(NSArray *)stripOutHttp:(NSString *)httpLine { // Setup an NSError object to catch any failures NSError *error = NULL; // create the

How to write regular expressions in Objective C (NSRegularExpression)?

雨燕双飞 提交于 2019-11-26 12:00:45
问题 I have this regex working when I test it in PHP but it doesn\'t work in Objective C: (?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?) I tried escaping the escape characters but that doesn\'t help either. Should I escape any other character? This is my code in Objective C: NSMutableString *searchedString = [NSMutableString stringWithString:@\"domain-name.tld.tld2\"]; NSError* error = nil; NSRegularExpression* regex = [NSRegularExpression

How to capture multiple repeated groups?

删除回忆录丶 提交于 2019-11-26 08:42:56
I need to capture multiple groups of the same pattern. Suppose, I have a following string: HELLO,THERE,WORLD And I've written a following pattern ^(?:([A-Z]+),?)+$ What I want it to do is, capture every single word, so that Group 1 is : "HELLO", Group 2 is "THERE" and Group 3 is "WORLD" What my regex is actually capturing only the last one, which is "WORLD". I'm testing my regular expression here and I want to use it with Swift (maybe there's a way in Swift to get intermediate results somehow, so that I can use them?) UPDATE: I don't want to use split . I just need to now how to capture all

How to capture multiple repeated groups?

好久不见. 提交于 2019-11-26 02:37:23
问题 I need to capture multiple groups of the same pattern. Suppose, I have a following string: HELLO,THERE,WORLD And I\'ve written a following pattern ^(?:([A-Z]+),?)+$ What I want it to do is, capture every single word, so that Group 1 is : \"HELLO\", Group 2 is \"THERE\" and Group 3 is \"WORLD\" What my regex is actually capturing only the last one, which is \"WORLD\". I\'m testing my regular expression here and I want to use it with Swift (maybe there\'s a way in Swift to get intermediate