Excluding strings from one string using Regex in iOS - 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"];

Related

How so I pull all chapters out from this NSSting using regular expression RegEx?

I'm trying to pull ANY characters out from the middle of this string (as seen in bold):
t_product_name:["xxx yyy zzz 111 222 333"],
Here is the code I'm trying but it's not working for me. What am I doing wrong with my RegEx?
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"t_product_name:[\"(.*)\"]" options:NSRegularExpressionCaseInsensitive error:&error];
[regex enumerateMatchesInString:html options:0 range:NSMakeRange(0, [html length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// detect
NSString *insideString = [html substringWithRange:[match rangeAtIndex:1]];
//print
NSLog(#"t_product_name: %#", insideString); }];
You should use lazy quantifier ? like this and escape [] double square brackets.
Regex: t_product_name:\[\"(.*?)\"\]
Regex101 Demo
Use a negated character class:
t_product_name:\["([^\"]+)"\]
The inner part says: do match everything except a double quote (it does not need to be escaped in square brackets but will need to be escaped in your string), see a demo on regex101.com.

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

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