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!
Matched:(null)Length:0error:ErrorDomain=RKLICURegexErrorDomainCode=66320UserInfo=0x10e060"There was an error compiling the regular expression.",{NSLocalizedDescription="Therewasanerrorcompilingtheregularexpression.";NSLocalizedFailureReason="TheerrorU_REGEX_INVALID_RANGEoccurredatline1,column25:][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 @".
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
Log:
If I remove the "@" I can get the following to work:
When you're having a problem like this, it's always useful to use the "full" method that includes the error: parameter. For example:
When run, it spits out:
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:
Note the - is escaped. This changes the meaning of the last few characters from "The characters from _ to @" TO "The individual characters _, -, and @".
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.