I'm new to iOS develoment, I have the data like this below and i want to remove all the empty lines, Please let me know what the pattern to be applied:
I have used the pattern as #"(\r\n)" and replaced it with #"", but it does not work. Please help me to sort out this issue
#EXTINF:-1 tvg-name="seedocs" tvg-logo="RT",RT
http://rt.ashttp14.visionip.tv/live/rt-global-live-HD/playlist.m3u8
#EXTINF:-1 tvg-name="hsn" tvg-logo="hsn",HSN TV
rtsp://hsn.mpl.miisolutions.net:1935/hsn-live01/_definst_/mp4:420p500kB31
#EXTINF:-1 tvg-name="us" tvg-logo="us",USTwit
http://bglive-a.bitgravity.com/twit/live/high
#EXTINF:-1 tvg-name="ALJAZEERA" tvg-logo="aljazeera",Aljazeera
rtmp://aljazeeraflashlivefs.fplive.net/aljazeeraflashlive-live/aljazeera_eng_high
#EXTINF:-1 tvg-name="bbc" tvg-logo="bbc",BBC World News
#EXTINF:-1 tvg-name="vevo" tvg-logo="vevo",Vevo
http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/06/prog_index.m3u8
#EXTINF:-1 tvg-name="vevo2" tvg-logo="vevo2",Vevo 2
http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch3/06/prog_index.m3u8
#EXTINF:-1 tvg-name="1HD" tvg-logo="1HD",1HD
rtmp://109.239.142.62/live/livestream3
[\r\n]+
You can use this instead.See demo.Replace by \n.
https://regex101.com/r/vV1wW6/26
NSString *string = #"Your multiline string";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"[\r\n]+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#"\n"];
Search: (?:\r?\n){2,}
Replace: \r\n
\r?\n is both Windows and Linux compatible. {2,} means "two or more instances"
demo
If supported, you can use \R instead of \r?\n to include other types of Unicode newlines. This may be useful in the future, if not at present.
It is possible to use the NSString method
stringByReplacingOccurrencesOfString:withString:options:range:
with the option:
NSRegularExpressionSearch.
There is no need to use NSRegularExpression.
NSString *cleanText = [originalText stringByReplacingOccurrencesOfString:#"[\r\n]+" withString:#"\n" options:NSRegularExpressionSearch range:NSMakeRange(0, text.length)];
This will replace all runs of any combinations of \r and/or \n with a single \n thus elimination all bank lines.
Related
I am reading text from a PDF to NSString. I replace all the spaces using the code below
NSString *pdfString = convertPDF(path);
pdfString=[pdfString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
pdfString=[pdfString stringByReplacingOccurrencesOfString:#"\r" withString:#""];
pdfString=[pdfString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
But this also eliminates paragraph spaces and multiple lines. I want to replace only a single occurrence of \n or \r and retain the paragraph spaces or multiple tabs and next lines.
There are two approaches:
Do a manual find in a loop
You can get the range of a string with -rangeOfCharactersFromSet:options:range:. The pearl of such a approach is to reduce the search range with every found match. Doing so you can simply compare the found range with the search range. If the found range is at the very beginning, it has been a double (or tripple) \r.
Get the individual components
With -componentsSeparatedByCharactersFromSet: (NSString) returns an array with strings separated with \r. Empty strings in this array are double (or triple) \r. Simply replace them with a \r and then rejoin the components with a space.
You should use NSRegularExpression to do this
NSString *pdfString = convertPDF(path);
//Replace all occurrences of \n by a single \n
NSRegularExpression *regexN = [NSRegularExpression regularExpressionWithPattern:#"\n" options:0 error:NULL];
pdfString = [regexN stringByReplacingMatchesInString:pdfString options:0 range:NSMakeRange(0, [pdfString length]) withTemplate:#"\n"];
//Replace all occurrences of \r by a single \r
NSRegularExpression *regexR = [NSRegularExpression regularExpressionWithPattern:#"\r" options:0 error:NULL];
pdfString = [regexR stringByReplacingMatchesInString:pdfString options:0 range:NSMakeRange(0, [pdfString length]) withTemplate:#"\r"];
Have you tried regex?
You can catch only the occurrences where an \n appears alone without another \n, then replace those occurrences with empty string:
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"[^\n]([\n])[^\n];" options:0 error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:#""];
I am trying to parse a string in Objective-C to delete an exact match of a specific #tagged word. I can create a regular expression and delete a specific word without issue, but when I try to remove a string with a leading "#" it's not working.
Here's my code:
NSString *originalString = #"This is just a #test that isn't working";
NSString *hashTag = #"#test";
NSString *placeholder = #"\\b%#\\b";
NSString *pattern = [NSString stringWithFormat:placeholder, hashTag];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:originalString
options:0
range:NSMakeRange(0, [originalString length])
withTemplate:#""];
The issue is that even if the original string includes the string #test, it's not removed. If I swap "#test" with "test", everything works fine, but that's not what I'm trying to do. What am I missing?
Because there isn't a word character exists between a space and #. Both are non-word characters. So i suggest you to remove the starting \\b
NSString *placeholder = #"%#\\b";
OR
Use a negative lookbehind.
NSString *placeholder = #"(?<!\\S)%#\\b";
(?<!\\S) Negative lookbehind which asserts that there isn't a non-space character exists before the match.
To do an exact string match, i suggest you to use this #"(?<!\\S)%#(?!\\S)" regex. (?!\\S) Negative lookahead which asserts that the match won't be followed by a non-space character.
DEMO
is it even necessary to use a regex, would this function not suffice?
[yourstring stringByReplacingOccurrencesOfString:#"#test" withString:#""];
I have tried (searching for) various possible solutions here on SO, in vain. Most of them simply replace all occurrences of backslashes, and don't respect backslashes that should otherwise be untouched.
For instance, if I have a Hi, it\'s me. How\'re you doing?, it should be Hi, it's me. How're you doing?. However, if someone tries to get creative with ASCII art, like
\\// \\// \\//
//\\ //\\ //\\
(WOW even SO won't let me add text as is, the above text needed extra backslashes to be displayed correctly.)
I cannot use [myString stringByReplacingOccurrencesOfString:#"\\" withString:#""]; since it will replace ALL backslashes. I do not want that.
I would like the string to be displayed as is.
NOTE: The strings in question here are values in NSDictionarys received as JSON from a web service. The use is in a service like a chat client, so it is important that text is handled correctly.
ULTRA IMPORTANT NOTE: I'm open to all ideas like library functions, regular expressions, human sacrifices, as long it gets the job done.
try this ...i cannot understand your question but it may help full for you,i think so
- (void)remove:(NSString*)str
{
NSString* const pattern = #"(\"[^\"]*\"|[^, ]+)";
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern
options:0
error:nil];
NSRange searchRange = NSMakeRange(0, [str length]);
NSArray *matches = [regex matchesInString:str
options:0
range:searchRange];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match range];
NSLog(#"%#", [str substringWithRange:matchRange]);
}
NSLog(#"%#",str);
}
call this method..
NSString* str = #"Hi, it\'s me. How\'re you doing?";
[self remove:str];
then the output is
Hi, it's me. How're you doing?
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"];
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 ;)