NSMutableString replaceOccurrencesOfString replacing whole words - ios

Is there a way to use replaceOccurrencesOfString (from NSMutableString) to replace whole words?
For example, if I want to replace all occurrences of a fraction in a string, like "1/2", I'd like that to match only that specific fraction. So if I had "11/2", I would not want that to match my "1/2" rule.
I've been trying to look for answers to this already, but I am having no luck.

You could use word boundaries \b with Regex. This example matches the "1/2" at the start and the end of the example string, but neither of the middle options
// Create your expression
NSString *string = #"1/2 of the 11/2 objects were 1/2ed in (1/2)";
NSError *error = nil;
NSRegularExpression *regex =
[NSRegularExpression
regularExpressionWithPattern:#"\\b1/2\\b"
options:NSRegularExpressionCaseInsensitive
error:&error];
// Replace the matches
NSString *modifiedString =
[regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:#"HALF USED TO BE HERE"];

Related

Delete occurances at the end of a string - iOS

Say you have a NSString *testString = #"Abcd!!!!";, note the four exclamation marks, how can I delete all exclamation marks as efficiently as possible?
The exclamation marks can be any number of amount, and can only be deleted if they're in consecutive trailing order.
One example might be:
NSString *testString = #"ABC!D!!!!!";
The result would then be:
NSString *result = #"ABC!D";
Since you don't know how many ! you'll be removing from the string, you could do it with a regular expression.
NSString *string = #"ABC!D!!!!!";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"!+$" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#""];
NSLog(#"%#", modifiedString);
Regex aren't always the most efficient way to solve these sorts of problems, but in this case, I don't think there would be a measurable gain doing it another way.

Regex to parse HTML Color

I'm trying to parse a HTML color components (#RRGGBB) using a regex, I'm trying this approach:
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:#"^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$"
options:NSRegularExpressionCaseInsensitive
error:&err];
NSArray *matches = [re matchesInString:hexColor
options:0
range:NSMakeRange(0, hexColor.length)];
but I think that can be simplified, so this next only stores the last match:
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:#"^#?([0-9a-fA-F]{2})+$"
options:NSRegularExpressionCaseInsensitive
error:&err];
NSArray *matches = [re matchesInString:hexColor
options:0
range:NSMakeRange(0, hexColor.length)];
so, what am I missing? how can the first approach be simplified? Thanks...
Edit
I had used the approach provided by #Arkanon:
NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:#"^#?((?:[a-fA-F0-9]{3}){1,2})$"
options:NSRegularExpressionCaseInsensitive
error:&err];
NSArray *matches = [re matchesInString:hexColor
options:0
range:NSMakeRange(0, hexColor.length)];
but I'm only matching one time, when I look for matches:
for(NSTextCheckingResult *match in matches) {
NSRange redRange = [match rangeAtIndex:1]; // first capture group (range is {1,6})
NSRange greenRange = [match rangeAtIndex:2]; // second capture group (throws an exception)
NSRange blueRange = [match rangeAtIndex:3]; // third capture group
}
How can I get each component using a simple regex? Thanks.
If you need to match the R, G and B components separately, then you must have three separate capture groups, so you can't get much more compact than your first regex. Add to this the fact that a CSS hexadecimal colour can be either three or six hex digits in length, and you will end up with an even more complex regex pattern if you need to match all valid CSS hex colour values.

RegEx - Detect specific strings before anything that isn't those strings [duplicate]

I have an NSString, let's say "H,L,K,P" how can I detect a specific character than then a wild-car character... for example, checking for ",*" would return ",L" ",K" and ",P" because they all have the specific "," and then they all have a character after them. Then I want to replace that string with itself plus a "period" appended to it.
So "H,L,K,P" would become "H,L.,K.,P."
Use a regular expression. The search pattern would be:
,(.)
the replacement pattern would be:
,$1.
Sample code:
NSString *string = #"H,L,K,P";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#",(.)"
options:0
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:#",$1."];

Replace characters (unique then wildcard value) in a string with other characters

I have an NSString, let's say "H,L,K,P" how can I detect a specific character than then a wild-car character... for example, checking for ",*" would return ",L" ",K" and ",P" because they all have the specific "," and then they all have a character after them. Then I want to replace that string with itself plus a "period" appended to it.
So "H,L,K,P" would become "H,L.,K.,P."
Use a regular expression. The search pattern would be:
,(.)
the replacement pattern would be:
,$1.
Sample code:
NSString *string = #"H,L,K,P";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#",(.)"
options:0
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:#",$1."];

Replace occurences of string that contains any number

Say I have a string that contains a control code "\f3" (yes it is RTF). I want to replace that control code with another string. At the moment I am using [mutableString replaceOccurrencesOfString:#"\f3" withString:#"replacement string" options:0 range:NSMakeRange(0, [mutableString length])];
This works fine, however sometimes the code can be "\f4" or even "\f12" for example. How do I replace these strings? I could use replaceOccurrencesOfString for each, but the better way to do it is using wildcards, as it could be any number.
Regular expressions would do it.
Take a look at NSRegularExpression (iOS >= 4) and this page for how regular expressions work.
You will want something like:
// Create your expression
NSError *error = nil;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:#"\\b\f[0-9]*\\b"
options:NSRegularExpressionCaseInsensitive
error:&error];
// Replace the matches
NSString *modifiedString = [regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:#"replacement string"];
WARNING : I've not tested my regaular expression and I'm not that great at getting them right first time; I just know that regular expressions are the way forward for you and it has to look something like that ;)

Resources