nsregularexpression

How to use NSRange or NSScanner in iOS

流过昼夜 提交于 2019-12-13 07:14:34
问题 I have a string like: 1 this is my first text 2 his is my second 3 this is my third 4 this is my forth etc etc.. Each sentence is between the numeric characters like 1 2 3 4 etc When I tap the first sentence, it needs to dissect it and copy it in clipboard or something. How can we use he NSScanner or NSRange to calculate the string size or identify strings between the numeric characters. self.multiPageView.text =combined; the combined contains the text like above ,,so when i tap the first

Why is my NSRegularExpression pattern not working?

橙三吉。 提交于 2019-12-13 04:40:58
问题 I have the following string: NSString *string = @"she seemed \x3cem\x3ereluctant\x3c/em\x3e to discuss the matter"; I want the final string to be: "she seemed reluctant to discuss the matter" I have the following pattern: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/\\x[0-9a-f]{2}/" options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])]; for

Extract 5 digit zip code from string

吃可爱长大的小学妹 提交于 2019-12-12 03:25:54
问题 I'm letting a user enter their address and I need to extract the zip code from it. I found that this RegEx should work: \d{5}([ \-]\d{4})? however I'm having an extremely difficult time getting this to work on Swift. This is where I"m at: private func sanatizeZipCodeString() -> String { let retVal = self.drugNameTextField.text let regEx = try! NSRegularExpression(pattern: "", options: .CaseInsensitive) let match = regEx.matchesInString(retVal!, options: [], range: NSMakeRange(0, (retVal?

“\P{Letter}” and NSRegularExpression

浪尽此生 提交于 2019-12-11 19:13:06
问题 I have an NSString I would like to test with an NSRegularExpression, but it doesn't act anything like I would expect and I don't know where to search for an answer anymore. So here is my code: BOOL companyNameContainsOnlyAuthorizedCharacters = YES; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\P{Letter}]" options:0 error:&error]; NSArray *matches = [[NSArray alloc] init]; matches = [regex matchesInString:[model editorName] options:0

Replacing numbers in an NSString with Objects from NSArray

霸气de小男生 提交于 2019-12-11 17:53:53
问题 In a UITextView users can enter numbers formatted like {{1}} or {{177}} . In a preview field, much like here on SO I'd like to replace that pattern with a value from an NSArray, where the number corresponds to the row in the array. I have two ideas to solve this: Count all occurrences with an NSRegularExpression, loop through them and replace them. Create a word-array out of the NSString and loop through the array, replacing occurrences and put the NSString back together. Which of those two

how to catch multiple instances special indicated **characters** in an NSString and bold them in between? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-11 11:46:23
问题 This question already has an answer here : How to catch special indicated **characters** in an NSString and bold what's in between? (1 answer) Closed 5 years ago . I'm having an issue finding multiple groups of substrings indicated by a pair of ** characters, and bolding them. for example in this NSString: The Fox has **ran** around the **corner** should read: The fox has ran around the corner here is my code: NSString *questionString = queryString; NSMutableAttributedString

NSRegularExpression is giving incorrect result from the numberOfRanges method

泄露秘密 提交于 2019-12-11 11:30:26
问题 My code is small Create a new XCode project and past this code to run: NSString *stringToCheck = @"## master...origin/master [ahead 1, behind 1]"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(ahead) (\\d), (behind) (\\d)\\]|\\[(behind) (\\d)\\]|\\[(ahead) (\\d)\\]" options:0 error:nil]; NSArray* matches = [regex matchesInString:stringToCheck options:0 range:NSMakeRange(0, [stringToCheck length])]; NSTextCheckingResult *match = [matches firstObject];

NSMutableString modify while iterating text checking results

本秂侑毒 提交于 2019-12-11 09:01:06
问题 I'm trying to modify an NSString while iterating NSTextCheckingResults from an NSRegularExpression . I know that it won't work the way I implemented it as every replacement changes the length of the string an so the validity of the NSRages in my loop. How can I replace multiple matches in a for loop? Here is my code: NSMutableString *string = [@"[H]…[mm]…[s]" mutableCopy]; NSReguralExpression *exp = [NSRegularExpression regularExpressionWithPattern:@"(\\[[Hms]{1,2}\\])" options:0 error:nil];

Ruby-style string interpolation with iOS NSRegularExpression

℡╲_俬逩灬. 提交于 2019-12-11 06:57:44
问题 I'm attempting to generate an HTML-output for a model in my app. This will effectively go through and fill in various spots inside an HTML file with the relevant values within the model. I was originally just going to take the HTML template as a formatted string, but if anything changes later on the layout or anything, this will just get really messy and tedious to match up the order of the values to the order they appear in the template. Instead, what I'm trying to do is run a sort of Ruby

convert javascript regex expression to swift nsregularexpression

混江龙づ霸主 提交于 2019-12-11 04:46:25
问题 I am new to swift programming. I have a valid regex in javascript, I need the same functionality in swift str.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''); Thanks 回答1: Swift 2.3: let str = "A+B/C====" let result = str.stringByReplacingOccurrencesOfString("+", withString: "-") .stringByReplacingOccurrencesOfString("/", withString: "_") .stringByReplacingOccurrencesOfString("\\=+$", withString: "", options: .RegularExpressionSearch) print(str) print(result) Swift 3: let str = "A