nsregularexpression

How can I use NSRegularExpression on Swift strings with variable-width Unicode characters?

梦想的初衷 提交于 2019-12-21 09:14:53
问题 I'm having trouble getting NSRegularExpression to match patterns on strings with wider (?) Unicode characters in them. It looks like the problem is the range parameter -- Swift counts individual Unicode characters, while Objective-C treats strings as if they're made up of UTF-16 code units. Here is my test string and two regular expressions: let str = "dog🐶🐮cow" let dogRegex = NSRegularExpression(pattern: "d.g", options: nil, error: nil)! let cowRegex = NSRegularExpression(pattern: "c.w",

Suggest # tags while typing (like Twitter) for iPhone UITextView

北战南征 提交于 2019-12-20 09:25:13
问题 I'd building an app that uses hashtags, like Twitter or Tweetbot. When you're typing a message, if you type the hashtag symbol, I'd like to suggest tags that match the current one you're typing. I've already figured out how to get the UITableView to appear and show a list of hashtags, but what I can't figure out is how to do the following: Get the NSRange of the current word being typed, See if that range is formatted like a hashtag ( NSRegularExpression @"#\\w\\w*" ) (From here on out, I've

What are the correct regular expressions for syntax highlighting?

谁说我不能喝 提交于 2019-12-20 07:49:32
问题 What are the correct regular expressions using NSRegularExpression for syntax highlighting? The following test must be passed: code // comment code /* comment */ code code "string" code code // comment code code /* comment comment */ code code "string code" string code "string\ string" code code // comment "comment" code /* comment "comment" */ code code "string /* string */ // string" code code // comment "string" code /* comment "comment" */ code code "string /* comment */ // comment"

Named capture groups with NSRegularExpression

偶尔善良 提交于 2019-12-18 11:40:37
问题 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. 回答1: 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:@"

Find multiple quoted words in a string with regex

拟墨画扇 提交于 2019-12-18 09:24:19
问题 My app supports 5 languages. I have a string which has some double quotes in it. This string is translated into 5 languages in the localizable.strings files. Example: title_identifier = "Hi \"how\", are \"you\""; I would like to bold out "how" and "you" in this string by finding the range of these words. So I am trying to fetch these quoted words out of the string and the result would be an array containing "how" and "you" or their range. func matches(for regex: String, in text: String) ->

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

廉价感情. 提交于 2019-12-18 04:09:35
问题 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

NSRegularExpression escaping parentheses

人盡茶涼 提交于 2019-12-13 18:28:05
问题 I'm using regular expressions to find some values in a string, however, what I'm trying to find looks something like this: Dealt to SanderDecler [2s 5d] But I can't seem to find a way to escape these square brackets, I've had the same problem with parentheses earlier. I've tried to escape them like this \( or \[, but that didn't give any matches. So I just replaced that with a dot, and it did match, however, that doesn't seem like the best way to do it, and I can imagine it's better for

Regex to match anchor tag and its href

风格不统一 提交于 2019-12-13 09:56:26
问题 I want to run regex through a html string that has multiple anchor tags and construct a dictionary of link text vs its href url. <p>This is a simple text with some embedded <a href="http://example.com/link/to/some/page?param1=77&param2=22">links</a>. This is a <a href="https://exmp.le/sample-page/?uu=1">different link</a>. How do I extract for <a> tag's text and href in one go? Edit: func extractLinks(html: String) -> Dictionary<String, String>? { do { let regex = try NSRegularExpression

Email Regular expression issue while using special characters [duplicate]

梦想的初衷 提交于 2019-12-13 08:55:16
问题 This question already has answers here : How to validate an email address using a regular expression? (73 answers) Closed 3 years ago . I have created the following regular expression for validating my email - (BOOL)isValidEmail { BOOL stricterFilter = YES; NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"; NSString *laxString = @".+@([A-Za-z0-9]+\\.)+[A-Za-z]{2}[A-Za-z]*"; NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;

Regular expression in Swift to validate Cardholder name

时光毁灭记忆、已成空白 提交于 2019-12-13 08:14:48
问题 I am looking for a regular expression to use in Swift to validate cardholder name for credit card. I am looking for a regEx which - - Has minimum 2 and maximum 26 characters - Accept dashes (-) and apostrophes (') only and no other special character - Capital and small alphabets and no numbers. - Should not start with a blank space. I was using this - "^[^-\\s][\\p{L}\\-'\\s]{2,26}$" but it only accepts dash (-) no apostrophe (') Thanks in advance for any help. 回答1: try with this regex (?<! )