Replace only single occurrence of \n or \r in NSString - ios

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:#""];

Related

iOS NSRegularExpression how to find the first matching "TAIL" of pattern like: HEAD(.*)TAIL

For example:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"hello: (.*)ABC" options:0 error:NULL];
NSString *str = #"hello: bobABC123ABC";
NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
NSLog(#"macthing part is %#", [str substringWithRange:[match rangeAtIndex:0]]);
The result of matching is "bobABC123ABC", so the matching of "ABC" in NSRegularExpression is finding the last "ABC" in the string instead of first.
I want the matching to be "bob",any one know how to achieve this?
Make you regular expression non-greedy. Say:
#"hello: (.*?)ABC"
^
|==> note this
instead of
#"hello: (.*)ABC"
From the documentation:
*? Match 0 or more times. Match as few times as possible.

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."];

NSMutableString replaceOccurrencesOfString replacing whole words

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"];

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."];

Excluding strings from one string using Regex in iOS

I want to do pretty much the same as in Excluding strings using regex but I want to do it iOS using Regex. So I basically I want to find matches in a string and then remove them from the string so if I have a string like this, Hello #world #something I want to find #world & #something and then remove them from the string so it just becomes Hello. I already have this expression that removes #world and something but not the #, #[\\p{Letter}]+|[^#]+$ I solved the # problem by doing this
NSString *stringWithoutAt = [input stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"#%#",atString] withString:#""];
NSString *stringWithoutTag = [input stringByReplacingOccurrencesOfString:tagString withString:#""];
So for the first one I end up with Hello #world and the second one Hello #something. But is there a way of using Regex or something else to remove both the #world and the #something at the same time?
You can use regex in iPhone in two ways:-
1>Using RegExKitLIte as framework see the tutorial
2>Using NSRegularExpression & NSTextCheckingResult
NSStirng *string=#"Your String";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"#[a-z]*#[a-z]*" options:NSRegularExpressionCaseInsensitive error:&error];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
{
// your statement if it matches
}];
Here any expression after# and expression after # is being concatenated
and in the statement you can replace it by space to get your expression
if u simply want modified string do this :-
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0
range:NSMakeRange(0, [string length]) withTemplate:#"$2$1"];

Resources