nsregularexpression

Youtube Video Id from URL - Swift3

冷暖自知 提交于 2019-12-06 03:32:23
问题 Basically I have a Youtube URL as string, I want to extract the video Id from that URL. I found some code in objective c that is as below: NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)" options:NSRegularExpressionCaseInsensitive error:&error]; NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])]; if (match) { NSRange videoIDRange = [match rangeAtIndex:1];

NSRegularExpression to extract text between two XML tags

核能气质少年 提交于 2019-12-05 23:29:26
问题 How to extract the value "6" between the "badgeCount" tags using NSRegularExpression. Following is the response from the server: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName> Following is the code I tried but not getting success. Actually it goes in else part and prints "Value of regex is nil": NSString *responseString = [[NSString alloc] initWithBytes:[responseDataForCrntUser bytes] length

Swift - Splitting strings with regex - ignoring search string

南楼画角 提交于 2019-12-05 21:25:46
There is a clever 1st answer given here for splitting swift string with a regex expression split string answer However it keeps the searched text within the array of answers. I'm trying to do a similar thing, but ignoring the characters acting as separators (e.g. just like the swift split function, but just with a regex expression as the separator). As an example: regex would be something like "\\\||Z|ZY" and when applied to string of "hi|thisZshouldZYbe|separated" then you would get back an array ["hi", "this", "should", "be", "separated"] NB. The regex is adapted to the swift

Does NSRegularExpression support partial case insensitive?

↘锁芯ラ 提交于 2019-12-05 18:21:07
As the title states, I wonder if NSRegularExpression in both Objective-c or Swift, support partial case insensitive search? Namely, will the pattern recognize (?ismx)? If not, is there a brief reason for this inability? I truly appreciate your explanation. From the NSRegularExpression Class Reference : Table 2 Regular Expression Operators ... (?ismwx-ismwx:...) Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled. ... (?ismwx-ismwx) Flag settings. Change the flag settings. Changes apply to the portion of the pattern following the setting. For

Swift Regex matching fails when source contains unicode characters

送分小仙女□ 提交于 2019-12-05 13:02:56
I'm trying to do a simple regex match using NSRegularExpression, but I'm having some problems matching the string when the source contains multibyte characters: let string = "D 9" // The following matches (any characters)(SPACE)(numbers)(any characters) let pattern = "([\\s\\S]*) ([0-9]*)(.*)" let slen : Int = string.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) var error: NSError? = nil var regex = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.DotMatchesLineSeparators, error: &error) var result = regex?.stringByReplacingMatchesInString(string, options: nil,

NSRegularExpression

拈花ヽ惹草 提交于 2019-12-05 06:22:05
问题 I have a string like: ?key=123%252Bf-34Fa&name=John ?name=Johon&key=123%252Bf-34Fa I want to get the value for the key ,I use this NSRegularExpression (?i)(?<=key=)[.?!&]+[?=&]?? What I think is that the pattern is like matching any character except "&".But the result is always NULL . the value of each key can have anything except "&". So How can I create the correct NSRegularExpression? thanks. 回答1: You shouldn't use a regex for this, specially if you don't know how. Compare: NSString

Named capture groups in NSRegularExpression - get a range's group's name

你离开我真会死。 提交于 2019-12-05 04:02:26
Apple says that NSRegularExpression is based on the ICU Regular Expression library: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSRegularExpression_Class/ The pattern syntax currently supported is that specified by ICU. The ICU regular expressions are described at http://userguide.icu-project.org/strings/regexp . That page (on icu-project.org) claims that Named Capture Groups are now supported, using the same syntax as .NET Regular Expressions: (?<name>...) Named capture group. The <angle brackets> are literal - they appear in the pattern. I have written a

NSRegularExpression for hashtags and mentions with special characters?

邮差的信 提交于 2019-12-04 17:40:13
I'm using the following regex expression to detect hashtags and mentions in my app. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|@)(\\w+)" options:NSRegularExpressionCaseInsensitive error:&error]; However users within my app are allowed to use some special characters in their usernames. For example @user.name or @user_name . Spaces are not allowed. However using thins regular expression would only detect @user when it should in fact be @user.name . Hostages work perfectly but the special characters in usernames break the mention functionality. I'm really

Replace regex matches in attributed string with image in Objective-C

a 夏天 提交于 2019-12-04 17:08:24
My goal is to store the information for an attributed string in Parse.com. I decided to come up with an encoding for attributed text for my images that works by replacing any string {X} in braces with the corresponding image. For example: Picture of 2 colorless mana: {X} Should produce an attributed string where {X} is replaced by an image. This is what I've tried: NSString *formattedText = @"This will cost {2}{PW}{PW} to cast."; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\{)[^}]+(?=\\})" options:NSRegularExpressionAnchorsMatchLines error:nil];

How do I repeat a capturing group?

江枫思渺然 提交于 2019-12-04 15:22:27
I have an input string that looks something like this: HLI6Ch60000Ch500C0Ch46400Ch30000Ch21888Ch10E79CS07LCU3Ch37880Ch27800Ch16480CS8CA00000000000000000000 Now I don't care about the part that follows the last letter A , it'll always be A and exactly 20 numbers that are of no use to me. I do, however, need the part before the last letter A , and ideally, I'd need it to be separated into two different captures, just like this: 1: HLI6Ch60000Ch500C0Ch46400Ch30000Ch21888Ch10E79CS07 2: LCU3Ch37880Ch27800Ch16480CS8C The only way to identify these matches is that they end with characters CS followed