1

I am trying to get the following regex to work on ios in order to make sure the user is only inputting numbers and a dot. I am not able to get number of matches to be above 0. I have also tried NSRange one as well and that will give me 0 no matter what as well, so my regex is not working, even thought I am pretty sure it should with what I have there. Any suggestions.

The Code I wrote is here with errorRegex is defined in the .h file and regError is defined as well.

errorRegex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9.]*" 
            options: NSRegularExpressionCaseInsensitive error:&regError];

NSUInteger rangeOfFirstMatch = [errorRegex numberOfMatchesInString:servAmount1TF.text 
            options:0 range:NSMakeRange(0, [servAmount1TF.text length])];
Rob
  • 426
  • 8
  • 23
  • I'm puzzled by your regex `@"\\b[^0-9^.]*\\b"`, in particular that second caret. Do you want the character class to match anything that is neither a digit nor a decimal point? If so, then I think you want `@"\\b[^0-9.]*\\b"`. – David Gorsline Jun 04 '12 at 14:02
  • Yes it is a redundant ^ which still worked in rubular's website so I dont think that is causing the actual error – Rob Jun 04 '12 at 14:34

3 Answers3

1

errorRegex is of type NSRegularExpression, but the error is of type UIButtonContent. This has all the halmarks of a memory error. Something in your code not going though a proper retain/release cycle.


I got a unit test to work with the expression @"[^0-9.]+"

- (void)testRE
{
    NSError *regError = nil;
    NSRegularExpression *errorRegex;
    NSString *string;
    NSUInteger count;

    errorRegex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9.]+" 
                                                           options: NSRegularExpressionCaseInsensitive
                                                             error:&regError];
    STAssertNil(regError, nil);

    string = @"00.0";
    count = [errorRegex numberOfMatchesInString:string 
                                        options:0
                                          range:NSMakeRange(0, [string length])];
    STAssertEquals(count, 0U, nil);

    string = @"00A00";
    count = [errorRegex numberOfMatchesInString:string 
                                        options:0
                                          range:NSMakeRange(0, [string length])];
    STAssertEquals(count, 1U, nil);
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • I fixed the part you commented on but forgot to update the post. I put all of the regex work in the same method instead of having the regex working globally in a .c file – Rob Jun 04 '12 at 19:11
  • Thank you. I added my answer because of the explanation and what the regexp represented so if others need it they will have the formula. – Rob Jun 27 '12 at 14:55
1
NSRegularExpression *errorCheckRegEx = [[NSRegularExpression alloc] initWithPattern:@"\\b^([0-9]+(\\.)?[0-9]*)$|^([0-9]*(\\.)?[0-9]+)$|^[0-9]*$|^([0-9]*(\\/)?[0-9]*)$\\b" options:NSRegularExpressionCaseInsensitive error:nil];
[match setArray: [errorCheckRegEx matchesInString:servAmount1TF.text options:0 range:NSMakeRange(0, [servAmount1TF.text length])]];

I figured out what I needed to do when I could finally get back to it so if anyone was interested this is what I came up with. The \b is just what ios uses in their regexp which is kind of dumb, but it will not work without that so I leave it there when it doesn't feel natural to do especially after ruby's example. This regular expression will get fractions, decimals -> .3; 2.3; 2; and does it from the front to end of the line. What I think might have been happening was the fact that I was not using the \b and also not matching correctly, which is the second line. Either way it works great now. Thanks for the help.

Rob
  • 426
  • 8
  • 23
1

Why not use stock-standard c's regex.h ?

See an example here: http://cboard.cprogramming.com/c-programming/117525-regex-h-extracting-matches.html

And more information here: https://stackoverflow.com/a/422159/1208218

Community
  • 1
  • 1
Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38
  • That is an interesting route. I choose the NS way because of iphone. But, I feel like this would be a better route in the end since I mean c works with objective-c so thank you. – Rob Jul 02 '12 at 12:58
  • Great to hear it was helpful. I assume there may be more things that could be done this way using stock-standard c features. – Roel Van de Paar Jul 13 '12 at 05:26