NSString Substring detection - ios

I need help with replacing occurrences of string with another string. Occurrency that needs to be detected is actually some kind of function:
%nx+a or %nx-a
where x and a are some numbers.
So for example %n10+2 or %n54-11.
I can't even use something like:
NSRange startRange = [snippetString rangeOfString:#"%n"];
because if I have two patterns within same string I'm checking I'll only get starting range of first one...
Thanks.

For something like this you could use an NSRegularExpression and use the method enumerateMatches:.
Or you can create your own loop.
The first is the easiest once you have the correct pattern.
Something like...
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"%n" options:0 error:nil];
NSString *string = #"%n10+2*%n2";
[regex enumerateMatchesInString:string
options:0
range:NSMakeRange(0, string.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// here you will get each instance of a match to the pattern
}];
You will have to check the docs for NSRegularExpression to learn how to do what work you need to do with this.
Docs... https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html

I assume that you need to do something with those two numbers. I think the best way is to use a regular expression to extract what you need in one go.
NSString * string = #"some %n5-3 string %n11+98";
NSError * regexError = nil;
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:#"%n(\\d+)([+-])(\\d+)"
options:0
error:&regexError];
NSArray * matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult * match in matches) {
NSString * firstNumber = [string substringWithRange:[match rangeAtIndex:1]];
NSString * secondNumber = [string substringWithRange:[match rangeAtIndex:3]];
NSString * sign = [string substringWithRange:[match rangeAtIndex:2]];
// Do something useful with the numbers.
}
Of course if you just need to replace all the %n occurences with a constant string you can do that in one call:
NSString * result = [string stringByReplacingOccurrencesOfString:#"%n\\d+[+-]\\d+"
withString:#"here be dragons"
options:NSRegularExpressionSearch
range:NSMakeRange(0, string.length)];
Disclaimer: I didn't test this code. Minor bugs may be present.

Alter this code to match ur need
yourString = [yourString stringByReplacingOccurrencesOfString:#" +" withString:#" "options:NSRegularExpressionSearch range:NSMakeRange(0, yourString.length)];

Related

Remove "?" from NSString IOS

I am trying to remove all symbols from a string, it works fine with below code for all symbols except "?".
NSString *newString = self.titleString;
NSArray *characters = #[#"<", #"!", #"#", #"#", #"$", #"%", #"^", #"&", #"*", #"(", #")", #",", #"_", #"+", #"|", #">", #"?", #" "];
for (NSString *str in characters) {
newString = [newString stringByReplacingOccurrencesOfString:str withString:#""];
}
You can use stringByReplacingOccurrencesOfString with the regular expression option, NSRegularExpressionSearch:
NSString *output = [input stringByReplacingOccurrencesOfString:#"[<>!##$%^&*(),_+?| ]" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, [input length])];
You can also take advantage of other regular expression features, e.g. replace all non-letter characters:
NSString *output = [input stringByReplacingOccurrencesOfString:#"\\P{L}" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, [input length])];
or not A-Z:
NSString *output = [input stringByReplacingOccurrencesOfString:#"[^a-zA-Z]" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, [input length])];
It just depends upon precisely what you're trying to achieve.
You can remove all characters in a single call using NSRegularExpression:
NSString *string = self.titleString;
NSError *error = nil;
// Prepare the regular expression that matches any of your characters
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"[<>!##$%^&*(),_+?| ]"
options:NSRegularExpressionCaseInsensitive
error:&error];
// Replace all matches with an empty string
string = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#""];
You have to escape the ? like that: "\?". Otherwise it won't work properly. ? only means in regex stands for 0 or 1 occurences and so you have to say to objective-c that it is a real char you want to test.
If this is for editing user text you might want to consider using a union of more than one NSCharacterSet - this will help it be more language-friendly when you localize/internationalize your app.
NSString *myInputText = #"asd;jkfhals#$%^%$&1asldiguhd";
NSCharacterSet *nonAlphanumeric = [[NSMutableCharacterSet alphanumericCharacterSet] invert];
NSArray *parts = [myInputText componentsSeparatedByCharactersInSet:nonAlphanumeric];
NSArray *mySafeOutputText = [parts componentsJoinedByString:#""];
It's a little non-intuitive to use the components method, but unfortunately Apple doesn't provide a stringByReplacingCharactersInSet: method. :(

Understanding how to use NSRegularExpressions correctly

I am trying to write a function that has an NSString and parses it returning an array of tags.
The definition of a tag is any nsstring text that starts with # and contains only alphanumeric characters after the #.
Is this correct?
#.*?[A-Za-z0-9]
I want to use matchesInString:options:range: but need some help.
My function is:
- (void) getTags
{
NSString* str = #"This is my string and a couple of #tags for #you.";
// Range is 0 to 48 (full length of string)
// NSArray should contain #tags and #you only.
Thanks!
The patten "#.*?[A-Za-z0-9]" matches a # which is followed by zero or more
characters which are not in the set [A-Za-z0-9]. What you probably want is
NSString *pattern = #"#[A-Za-z0-9]+";
The you can create a regular expression using that pattern:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
and enumerate all matches in the string:
NSString *string = #"abc #tag1 def #tag2.";
NSMutableArray *tags = [NSMutableArray array];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [result range];
NSString *tag = [string substringWithRange:range];
[tags addObject:tag];
}];
NSLog(#"%#", tags);
Output:
(
"#tag1",
"#tag2"
)

How do I extract a specific part of a string with NSRegularExpression?

Say I have a bunch of different strings:
"http://website.com/283/comments/einp43/2398/32/34/23/4/4"
"http://website.com/23283/l34/comments/inhd3/3928/3/2/3"
"http://website.com/pics/283/comments/en43/a89st/389238/a823/"
"http://website.com/pics/hd/283/comments/as87/asd7j/3"
And I always want the portion that follows comments/ which is a valuable ID. But I don't want the comments part, I just want the ID.
How do I isolate/extract that?
Assuming the website name is stored as an NSString called websiteName:
NSArray *components = [websiteName componentsSeparatedByString:#"comments/"];
NSString *valuableID = [components lastObject];
(Edit: I didn't notice the mention of NSRegularExpression in the title until after posting this answer, but I don't think regex is necessary in this case since you're looking for a single constant string and have no need for complex pattern recognition.)
You can do it with regex, try this code
NSString *string = #"http://website.com/23283/l34/comments/inhd3/3928/3/2/3";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"/comments/([^/]*)" options:NSRegularExpressionCaseInsensitive error:nil];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSString *subStr = [string substringWithRange:[result rangeAtIndex:1]];
NSLog(#"commentId = %#", subStr);
}];
output:
commentId = inhd3

iOS: extract substring of NSString in objective C

I have an NSString as:
"<a href='javascript:void(null)' onclick='handleCommandForAnchor(this, 10);return false;'>12321<\/a>"
I need to extract the 12321 near the end of the NSString from it and store.
First I tried
NSString *shipNumHtml=[mValues objectAtIndex:1];
NSInteger htmlLen=[shipNumHtml length];
NSString *shipNum=[[shipNumHtml substringFromIndex:htmlLen-12]substringToIndex:8];
But then I found out that number 12321 can be of variable length.
I can't find a method like java's indexOf() to find the '>' and '<' and then find substring with those indices. All the answers I've found on SO either know what substring to search for or know the location if the substring. Any help?
I don't usually advocate using Regular expressions for parsing HTML contents but it seems a regex matching >(\d+)< would to the job in this simple string.
Here is a simple example:
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#">(\\d+)<"
options:0
error:&error];
// Handle error != nil
NSTextCheckingResult *match = [regex firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])];
if (match) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *number = [string substringWithRange:matchRange]
NSLog(#"Number: %#", number);
}
As #HaneTV says, you can use the NSString method rangeOfString to search for substrings. Given that the characters ">" and "<" appear in multiple places in your string, so you might want to take a look at NSRegularExpression and/or NSScanner.
that may help on you a bit, I've just tested:
NSString *_string = #"<a href='javascript:void(null)' onclick='handleCommandForAnchor(this, 10);return false;'>12321</a>";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:#">(.*)<" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matchesInString = [_regExp matchesInString:_string options:NSMatchingReportCompletion range:NSMakeRange(0, _string.length)];
[_matchesInString enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
for (int i = 0; i < result.numberOfRanges; i++) {
NSString *_match = [_string substringWithRange:[result rangeAtIndex:i]];
NSLog(#"%#", _match);
}
}];

How to get all strings inside [...] in one NSString?

Say given an NSString:
#"[myLabel]-10-[youImageView]"
I need an array of:
#[#"myLabel", #"yourImageView"]
How do I do it?
I thought about going through the string and check each '[' and ']', get string inside them, but is there any other better way?
Thanks
You can use regular expressions:
NSString *string = #"[myLabel]-10-[youImageView]";
// Regular expression to find "word characters" enclosed by [...]:
NSString *pattern = #"\\[(\\w+)\\]";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:NULL];
NSMutableArray *list = [NSMutableArray array];
[regex enumerateMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// range = location of the regex capture group "(\\w+)" in the string:
NSRange range = [result rangeAtIndex:1];
[list addObject:[string substringWithRange:range]];
}
];
NSLog(#"%#", list);
Output:
(
myLabel,
youImageView
)
Would this work for you?
NSCharacterSet *aSet = [NSCharacterSet characterSetWithCharactersInString:#"]-10["];
NSArray *anArray = [aString componentsSeparatedByCharactersInSet:aSet];

Resources