nsregularexpression

iOS Regex: Unknown escape sequence “\\|”

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 06:36:49
I'm getting a weird warning, and as a result my regex search isn't working. Here's the line: NSRange r = [HTML rangeOfString:@"\|(.*)\|" options:NSRegularExpressionSearch]; Where HTML is a string that I'm sure contains a single match for the above regex. The warning is only on the first occurrence of "\|", not on both. Any help is much appreciated! You're getting the warning because \| is not a valid escape sequence in Objective-C (or C or C++ for that matter). The compiler is ignoring that and just using a raw | character instead, so the string you're actually passing in is @"|(.*)|" . To get

Named capture groups with NSRegularExpression

*爱你&永不变心* 提交于 2019-11-30 04:01:33
Does NSRegularExpression support named capture groups? It doesn't look like it from the documentation , but I wanted to check before I explore alternative solutions. Named grouping is not supported in iOS, all you can do as I see is to make use of Enum : Enum: typedef enum { kDayGroup = 1, kMonthGroup, kYearGroup } RegexDateGroupsSequence; Sample Code: NSString *string = @"07-12-2014"; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d{2})\\-(\\d{2})\\-(\\d{4}|\\d{2})" options:NSRegularExpressionCaseInsensitive error:&error]; NSArray

Using regex to match date format in yyyymmdd

淺唱寂寞╮ 提交于 2019-11-30 03:45:58
问题 The regex should match valid dates in a string in the format YYYYMMDD . For example, aaa_20150327_bbb should be matched but aaa_20150229_bbb not because 2015 is not a leap year. Only year from 2000 to 2099 need to be considered. 回答1: Total madness (years 0-9999) The following one (based on this answer) works for years between 0 and 9999. (?<!\d)(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[1,3-9]|1[0-2])(?:29|30)))|(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579]

Regular expression in ios to extract href url and discard rest of anchor tag?

自古美人都是妖i 提交于 2019-11-29 09:55:54
问题 I want to write a url extracting function in objective C. The input text can be anything and may or may not contain html anchor tags. Consider this: NSString* input1 = @"This is cool site <a href="https://abc.com/coolstuff"> Have fun exploring </a>"; NSString* input2 = @"This is cool site <a target="_blank" href="https://abc.com/coolstuff"> Must visit </a>"; NSString* input3 = @"This is cool site <a href="https://abc.com/coolstuff" target="_blank" > Try now </a>"; I want modified string as

iOS Regex: Unknown escape sequence “\|”

帅比萌擦擦* 提交于 2019-11-29 06:15:51
问题 I'm getting a weird warning, and as a result my regex search isn't working. Here's the line: NSRange r = [HTML rangeOfString:@"\|(.*)\|" options:NSRegularExpressionSearch]; Where HTML is a string that I'm sure contains a single match for the above regex. The warning is only on the first occurrence of "\|", not on both. Any help is much appreciated! 回答1: You're getting the warning because \| is not a valid escape sequence in Objective-C (or C or C++ for that matter). The compiler is ignoring

Is there an Objective-c regex replace with callback/C# MatchEvaluator equivalent?

荒凉一梦 提交于 2019-11-29 04:19:29
I have a C# project I'm intending to port to Objective-C. From what I understand about Obj-C, it looks like there's a confusing variety of Regex options but I can't see anything about a way of doing a replace with callback. I'm looking for something that is the equivalent of the C# MatchEvaluator delegate or PHP's preg_replace_callback. An example of what I want to do in C# is - // change input so each word is followed a number showing how many letters it has string inputString = "Hello, how are you today ?"; Regex theRegex = new Regex(@"\w+"); string outputString = theRegex.Replace

Check if string contains special characters in Swift

不打扰是莪最后的温柔 提交于 2019-11-28 16:43:58
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. Your code check if no character in the string is from the given set. What you want is to check if any

Ignore escaped double quote characters swift

有些话、适合烂在心里 提交于 2019-11-28 08:57:31
问题 I am trying to validate a phone number using NSPredicate and regex. The only problem is when setting the regex Swift thinks that I am trying to escape part of it due to the backslashes. How can I get around this? My code is as follows: let phoneRegEx = "^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$" 回答1: In Swift, you still need to double-escape the slashes in string literals to define literal backslashes: let phoneRegEx

Capture groups not working in NSRegularExpression

故事扮演 提交于 2019-11-28 04:53:04
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:NSRegularExpressionSearch error:nil]; NSArray *matches = [nameExpression matchesInString:htmlString options:0 range

Cocoa error 2048 when using NSRegularExpression in Cocoa

℡╲_俬逩灬. 提交于 2019-11-28 04:38:45
问题 I'm building a regular expression for use in a parser in an iOS app. Here's my code: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=#EXT[^\r\n]*[\r\n]+)[^#][^\r\n]+" options:NSRegularExpressionAnchorsMatchLines error:&regexError ]; if (regexError) { NSLog(@"regexError: %@", regexError); return nil; } From this answer. This gives out this error: regexError: Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)"