Menu

Matching an "@" at sign

Anonymous
2010-01-29
2013-04-24
  • Anonymous

    Anonymous - 2010-01-29

    Hello.  I'm working on an iPhone project and would like to verify that the user has input a textfield properly (it's a username) due to some restrictions on the primary site where this username will be sent.  I.e, I want to be a good neighbor and not send junk data to them. 

    I can't seem to match an @ sign.  Whenever I put this into the list of characters to match it always fails.  I don't believe that I need it  escaped, but I have tried it both single and double escaped just in case.  (\@  and \\@)

    If you have an idea of what I'm doing wrong I would certainly appreciate the help.  If you need more information to help, please let me know and I'll get it  Thank you!

    Code:

        NSString *userNameCorrect = [userName.text stringByMatching:@"(^[a-zA-Z][a-zA-Z0-9\\._-@]{1,30}$)" capture:1L];
        NSLog(@"Matched:  %@  Length: %D", userNameCorrect, [userNameCorrect length]);
    

    Log:

    2010-01-29 10:48:11.544 Name[17779:4903] Matched:  (null)  Length: 0
    

    If I remove the "@" I can get the following to work:

    2010-01-29 10:52:05.945 Name[17871:531b] Matched:  ingo-more_hi.wow  Length: 16
    
     
  • John Engelhart

    John Engelhart - 2010-01-29

    When you're having a problem like this, it's always useful to use the "full" method that includes the error: parameter.  For example:

    NSError *error = NULL;
    NSString *userNameCorrect = [userName stringByMatching:@"(^[a-zA-Z][a-zA-Z0-9\\._-@]{1,30}$)" options:0 inRange:NSMakeRange(0UL, [userName length]) capture:1L error:&error];
    NSLog(@"Matched:  %@  Length: %D error: %@, %@", userNameCorrect, [userNameCorrect length], error, [error userInfo]);
    

    When run, it spits out:

    Matched:  (null)  Length: 0 error: Error Domain=RKLICURegexErrorDomain Code=66320 UserInfo=0x10e060 "There was an error compiling the regular expression.", {
        NSLocalizedDescription = "There was an error compiling the regular expression.";
        NSLocalizedFailureReason = "The error U_REGEX_INVALID_RANGE occurred at line 1, column 25: ][a-zA-Z0-9\\._-<<HERE>>@]{1,30}$)";
        RKLICURegexErrorCode = 66320;
        RKLICURegexErrorName = "U_REGEX_INVALID_RANGE";
        RKLICURegexLine = 1;
        RKLICURegexOffset = 25;
        RKLICURegexPostContext = "@]{1,30}$)";
        RKLICURegexPreContext = "][a-zA-Z0-9\\._-";
        RKLICURegexRegex = "(^[a-zA-Z][a-zA-Z0-9\\._-@]{1,30}$)";
        RKLICURegexRegexOptions = 0;
    }
    

    The problem is at "_-@", which is saying "Match the range of characters from _ to @" (_ = 95, @ = 64).  Character ranges must always be positive.

    Given the context, I suspect the regex you really want is:

    @"(^[a-zA-Z][a-zA-Z0-9\\._\\-@]{1,30}$)"
    

    Note the - is escaped.  This changes the meaning of the last few characters from "The characters from _ to @" TO "The individual characters _, -, and @".

     
  • Anonymous

    Anonymous - 2010-01-29

    Thank you jengelhart!  I guess this is one of those times when you need another set of eyes on something.  Because I wasn't trying to do a range just the literal of "-" and the error was only coming with the "@" I never even thought about escaping the preceding character.  I'm glad you figured that out from my example or this might have taken another 5 messages.  :)

    I will include the full method going forward, not just what I'm using and thank you for that advice.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.