验证用的sscanf()格式说明符的电子邮件地址 [英] Validating an email address with sscanf() format specifiers

查看:223
本文介绍了验证用的sscanf()格式说明符的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是有点的修复我的 - code的问题,但我看的文档,<一个href=\"https://www.google.com/search?q=sscanf%20format%20specifiers%20in%20c&oq=ssc&aqs=chrome.2.69i57j69i35l2j0l2j69i61.4400j0j7&sourceid=chrome&es_sm=93&ie=UTF-8\"相对=nofollow>范例和几十 ,<一个href=\"http://stackoverflow.com/questions/201323/using-a-regular-ex$p$pssion-to-validate-an-email-address\">of, <一href=\"http://stackoverflow.com/questions/182542/email-address-validation-for-asp-net?rq=1\">related, <一href=\"http://programmers.stackexchange.com/questions/78353/how-far-should-one-take-e-mail-address-validation/78359#78359\">questions,虽然我理解逻辑或多或少它是如何工作,我无法将其转化为一个C 的sscanf()格式code。我还是比较新的C,和我刚开始进入小幅超越简单化的东西,我有麻烦找出更复杂的格式说明(即%[^ ...] ,等等)。

This may be somewhat of a "fix-my-code" question, but I've looked at documentation, examples, and dozens, of, related, questions, and though I logically understand more or less how it all works, I am having trouble translating it into a C sscanf() format code. I am still relatively new to C, and am just starting to get into slightly beyond-simplistic stuff, and I am having trouble figuring out more complex format specifiers (ie. %[^...], etc.).

不管怎么说,这里是我的:

Anyways, here's what I have:

char user[EMAIL_LEN];
char site[EMAIL_LEN];
char domain[4];
if(sscanf(input, "%s@%s.%3s", user, site, domain) != 3){
  printf("--ERROR: Invalid email address.--\n");
}

为什么不工作的?我只是试图让一个简单的 aaaa@bbbb.ccc 格式,但由于某些原因的sscanf(输入%s @%S。 %3秒,用户,站点,域)始终计算为 1 。我是否需要使用一些疯狂的%[^ ...] 魔法为它正确地转换?我一直在搞乱%[^ @] 和之类的话,但我似乎无法使它工作。

Why doesn't that work? I'm just trying to get a simple aaaa@bbbb.ccc format, but for some reason sscanf(input, "%s@%s.%3s", user, site, domain) always evaluates to 1. Do I need to use some crazy %[^...] magic for it to convert correctly? I've been messing with %[^@] and that kind of thing, but I can't seem to make it work.

任何及所有帮助AP preciated。谢谢!

Any and all help is appreciated. Thanks!

推荐答案

%S 在scanf格式跳过前导空白,那么所有非空白字符相匹配并不包括下一个空格系统字符。因此,当你给它您的电子邮件地址,然后整个地址被复制到用户来匹配%S 。那么,作为下一个字符不是 @ ,仅此而已匹配和 scanf函数返回1​​。

%s in a scanf format skips leading whitespace, then matches all non-whitespace characters up to and not including the next whitespace charater. So when you feed it your email address, then ENTIRE address gets copied into user to match the %s. Then, as the next character is not @, nothing more is matched and scanf returns 1.

您可以尝试使用类似:

sscanf(input, "%[^@ \t\n]@%[^. \t\n].%3[^ \t\n]", user, site, domain)

这将匹配一切都交给一个 @ 或空格作为用户,那么,如果下一个字符,其实是一个 @ 将跳过它,并保存一切都交给或空格在网​​站。但是,这会接受很多不在一个电子邮件地址有效的其它字符,并且不会接受较长域名。更可能是这样的:

this will match everything up to a @ or whitespace as the user, then, if the next character is in fact a an @ will skip it and store everything up to . or whitespace in site. But this will accept lots of other characters that are not valid in an email address, and won't accept longer domain names. Better might be something like:

sscanf(input, "%[_a-zA-Z0-9.]@%[_a-zA-Z0-9.]", user, domain)

这将接受字母任何字符串,数字,下划线和周期的名称和域名两者。然后,如果你真的需要分拆域的最后一部分,这样做分开。

which will accept any string of letters, digits, underscore and period for both the name and domain. Then, if you really need to split off the last part of the domain, do that separately.

这篇关于验证用的sscanf()格式说明符的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆