stringByReplacingOccurrencesOfString Regular Expression - objective-c

I'm developing a Mac app and I'm trying to replace use NSString's stringByReplacingOccurrencesOfString. I'm doing something like:
NSString *new = [s stringByReplacingOccurrencesOfString:#"(special-tag)*.*</body" withString:html];
On an NSString. But whenever I try to use this function with a regular expression it seems to break. Is there something I'm missing? I found a few external regex libraries, but I'd rather use something built in that has similar functionality.
Any advice? Thanks in advance! EDIT - I know why it's breaking, I need help figuring out how to do an NSString replace with regular expressions

As the name suggest stringByReplacingOccurrencesOfString OccurrencesOfString it's a string not a RegEx. So it will replace your string rather than your RegEx.
-----------Edited-----------------
I haven't used regex before hope this will give you the idea
NSString *string = #"this is your string";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\\b(a|b)(c|d)\\b" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#"$2$1"];
Here is the NSRegularExpression Class Reference

Take a look at the NSRegularExpression class. It sounds like the -stringByReplacingMatchesInString:options:range:withTemplate: method will fit your needs. You might also like –replaceMatchesInString:options:range:withTemplate:.

Related

Why is my NSRegularExpression pattern not working?

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 (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSLog(#"%#", NSStringFromRange(matchRange));
}
However, I get an error saying the pattern is invalid. What am I doing wrong?
The pattern you need is:
#"\\\\x[0-9a-f]{2}"
The backslash is special to both Obj-C and the RE parser - so you need to create an Obj-C string with two \'s so the RE parser can then end up with one.
Also there are no open/close delimiters in the string - you're thinking of another programming language there!
You can save yourself some regex troubles by using the NSString method
stringByReplacingOccurrencesOfString:withString:
Or
stringByReplacingOccurrencesOfString:withString:options:range:

NSRegularExpression get only the regex

i have a problem and i don't undestand how to do this ( after 6hours or googling)
i'have a string named "filename" containt this text :"Aachen-Merzbrück EDKA\r\r\nVerkehr"
i want to use regex to only get this part "Aachen-Merzbrück EDKA" but i cant....
here my code :
NSString *expression = #"\\w+\\s[A-Z]{4}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSString *noAirportString = [regex stringByReplacingMatchesInString:filename options:0 range:NSMakeRange(0, [filename length]) withTemplate:#""];
EDIT :
this one work good :
\S+\s+[A-Z]{4}
but now, how to get only this "Aachen-Merzbrück" EDKA from "Aachen-Merzbrück EDKA\r\r\nVerkehr"
my regex with NSRegularExpression return me the same string ....
A couple of issues in your question:
No need to match city name characters - there are always weird ones around (hyphens, apostrophes, etc.) You can just match the first "line" in your text with a test for the ICAO code as an extra security.
Using stringByReplacingMatchesInString: you actually remove the airport name (and ICAO code) that you want keep.
stringByReplacingMatchesInString: is a hacky (because it deletes things, so you need to make your regexes "negative") shortcut that sometimes works (I use it myself) but which risks confusing things - and future readers.
Having said that, a few changes will fix it:
NSString *filename = #"Aachen-Merzbrück EDKA\r\r\nVerkehr";
// Match anything from the beginning of the line up to a space and 4 upper case letters.
NSString *expression = #"^.+\\s[A-Z]{4}$";
NSError *error = NULL;
//Make sure ^ and $ match line endings,
//and make it case sensitive (the default) to explicitly
//match the 4 upper case characters of the ICAO code
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionAnchorsMatchLines error:&error];
NSArray *matches = [regex matchesInString:filename
options:0
range:NSMakeRange(0, [filename length])];
// Check that there _is_ a match before you continue
if (matches.count == 0) {
// Error
}
NSRange airportNameRange = [[matches objectAtIndex: 0] range];
NSString *airportString = [filename substringWithRange: airportNameRange];
Thanks it's good working, but i use this one, it's work better in my case :
NSString *expression = #"\\S+\\s+[A-Z]{4}";

Dealing with separation characters within quotes when using componentsSeparatedByCharactersInSet

I'm trying to separate a string by the use of a comma. However I do not want to include commas that are within quoted areas. What is the best way of going about this in Objective-C?
An example of what I am dealing with is:
["someRandomNumber","Some Other Info","This quotes area, has a comma",...]
Any help would be greatly appreciated.
Regular expressions might work well for this, depending on your requirements. For example, if you're always trying to match items that are enclosed in double quotes, then the it might be easier to look for the quotes rather than worrying about the commas.
For example, you could do something like this:
NSString *pattern = #"\"[^\"]*\"";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NString *substring = [string substringWithRange:matchRange];
// do whatever you need to do with the substring
}
This code looks for a sequence of characters enclosed in quotes (the regex pattern "[^"]*"). Then for each match it extracts the matched range as a substring.
If that doesn't exactly match your requirements, it shouldn't be too difficult to adapt it to use a different regex pattern.
I'm not in a position to test this code at the moment, so my apologies if there are any errors. Hopefully the basic concept should be clear.

NSRegularExpression to match and replace all occurencies (porting from Ruby lang)

I have troubles while trying to port the Ruby code to the ObjC code
Ruby:
clean_url = original_url.gsub(/\\u0026[^&]*/, "")
Execution:
original_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1\u0026quality=medium\u0026type=video/webm&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794\u0026fallback_host=tc.v19.cache7.c.youtube.com,url=http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0
clean_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794
Ruby code works as expected.
ObjC code:
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"\\u0026[^&]*" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *originalUrl = #"http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1\\u0026quality=medium\\u0026type=video/webm&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794\\u0026fallback_host=tc.v19.cache7.c.youtube.com,url=http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0";
NSString *cleanUrl = [regex stringByReplacingMatchesInString:originalUrl options:0 range:NSMakeRange(0, [originalUrl length]) withTemplate:#"bla"];
NOTICE on withTemplate:#"bla" because without it we cannot see where is the problem.
Execution:
clean_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0blablablablablablablablablablablablablablablablabla
Thanks in advance!
The primary problem is your regular expression. It needs to be:
#"\\\\u0026[^&]*"
You want two backslashes in the regular expression. In C and Objective-C, a backslash needs to be escaped with another backslash. This means the string needs 4 backslashes.
Here's a simpler approach if you only need to process one string:
NSString *originalUrl = #"http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1\\u0026quality=medium\\u0026type=video/webm&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794\\u0026fallback_host=tc.v19.cache7.c.youtube.com,url=http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0";
NSString *cleanURL = [originalURL stringByReplacingOccurrencesOfString:#"\\\\u0026[^&]*" withString:#"" options: NSRegularExpressionSearch range:NSMakeRange(0, originalURL.length)];
If you need to process multiple strings with the regular expression then using NSRegularExpression is more efficient.

Regex help in objective c

<NSSimpleRegularExpressionCheckingResult: 0x100217890>{0, 4}{<NSRegularExpression: 0x100214700> test 0x1}
This is one element in the array which stores the result of a regular expression search.
I've got what I want: 'test'. I don't however want all the stuff around it ie
<NSSimpleRegularExpressionCheckingResult: 0x100217890>{0, 4}{<NSRegularExpression: 0x100214700> etc
I've got a feeling I'm going to have to send something to this element ie
[element stringValue];
but I need a little help discovering what that is..
My full code is below:
NSString *test = #"test 123 test";
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:#"test" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* result = [regex matchesInString:test options:0 range:NSMakeRange(0, [test length])];
NSLog(#" %#", [result objectAtIndex:0]);
which puts out
<NSSimpleRegularExpressionCheckingResult: 0x105b17890>{0, 4}{<NSRegularExpression: 0x105b14700> test 0x1}
Thanks!!
[regex matchesInString...
gives you NSArray of NSTextCheckingResults.
Maybe you'd want to use firstMatchInSting:options:range:
It will give you NSTextCheckingResult, from which you can get range (NSRange) which you apply to your string with substringWithRange: method.
I hope you can understand. If not - I'll explain more carefully.
Nevertheless, read NSRegularExpression reference and NSTextCheckingResult reference
Okay. I'm making process. If I send 'regularExpression' to the element, it narrow the result down to
<NSRegularExpression: 0x106214700> test 0x1
I'm aware that this is the print-out of an object but I am still unsure how to isolate the text!