NSRegularExpression, specify case-sensitive match? - ios

Is there a way using NSRegularExpression to specify that you want to do a case-sensitive search? I am trying to match the upper-case TAG "ACL" in the text below. The pattern I am using is simply:
// Pattern
[A-Z]+
// SearchText
<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>
// Code:
NSString *textBuffer = #"<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>";
NSString *pattern = #"([A-Z]+)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult *result = [regExp firstMatchInString:textBuffer options:0 range:NSMakeRange(0, [textBuffer length])];
NSLog(#"OBJECT CLASS: %#", [textBuffer substringWithRange:[result range]]);
Output: (with case-Insensative I am getting the first "td" as expected, when what I really want is "ACL"
I know that NSRegularExpressionCaseInsensitive is wrong, I was hoping there would be a NSRegularExpressionCaseSensitive. Also there is a flagOption ?(i) that also specifies a case-insensitive search but again nothing for case-sensative. What am I missing?

Case sensitive is the default. Dont put the insensitive flag in there.

Related

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.

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 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