I want to match number-number expression.I want to get input from textField, and need to format in way that,i can use that two numbers
How can i do this, by using regular expression or something else. Please give me a suggestion. User input. Number-Number. I have to filter out. number, number as Integer. Also i have to check whether user entered anything wrong in textfield.
If you use NSRegularExpression,
NSString *userInputString = #"1990-2020";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"(\\d+)-(\\d+)" options:0 error:nil];
NSArray *matches = [regex matchesInString:userInputString options:NSMatchingReportProgress range:NSMakeRange(0, userInputString.length)];
for (NSTextCheckingResult *match in matches)
{
NSLog(#"%#", [userInputString substringWithRange:[match rangeAtIndex:1]]);
NSLog(#"%#", [userInputString substringWithRange:[match rangeAtIndex:2]]);
}
output:
1990
2020
Related
I am using flickrApi in iPhone. After authenticiation, i get their userId as
inNSID = (_NSCFString) #"108346178%40N06"
Note the %40
In my app, I am saving the same id in my server. At some other place, where i have to authenticate again, i use this id to check if authentication is already done or not., when i try to compare this string with my string using isEqualToString , the result is NO.
Here is log trace
Printing description of tempAcc->serviceId:
108346178%40N06
Printing description of inNSID:
108346178%40N06
Both seems to be exactly same, how can I compare both? What is going on here.
Use NSRegularexpression to scan Unicode strings for any matches.
NSString *string = #"108346178%40N06";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:string
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:string
options:NSMatchingReportCompletion
range:NSMakeRange(0, string.length)];
if (match) {
NSLog(#"yes");
} else {
NSLog(#"no");
}
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.
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.
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."];
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.