将电子邮件地址读取为令牌
如何读取电子邮件地址作为令牌?
我看到 tokenizer 方法有 16 位长度的限制,我的令牌是这样的:
command [email protected] 50
我希望能够存储电子邮件(可以是任何电子邮件地址)和数字(可以在 5-1500 之间变化)。我不关心命令令牌。
我的代码如下所示:
String test2 = command.substring(7);
StringTokenizer st = new StringTokenizer(test2);
String email = st.nextToken();
String amount = st.nextToken();
How can I read an email address as a token?
I saw that the tokenizer method has a limit of 16 bits of length, well my token is like this:
command [email protected] 50
I want to be able to store the email (can be any email address) and the number (can vary from 5-1500). I dont care about the command token.
My code looks like this:
String test2 = command.substring(7);
StringTokenizer st = new StringTokenizer(test2);
String email = st.nextToken();
String amount = st.nextToken();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StringTokenizer
不是完成这里任务的工具。电子邮件对于它来说太复杂了,无法处理,因为它无法将本地部分是带引号字符串的有效电子邮件地址视为一个标记:请改用解析器生成器。许多都有非常好的 RFC 2822 语法。
例如,http://users.erols.com/blilly/mparse/rfc2822grammar_simplified.txt< /a> 定义了
addr-spec
这是你想要的产生式,你可以为 a 定义一个语法产生式命令、空格、地址规范、空格、数字,然后将您的顶级生成定义为一系列由换行符分隔的生成。StringTokenizer
is not the tool for the job here. Emails are just too complex for it to handle since it is not going to be able to treat valid email addresses where the local-part is a quoted-string as one token:Use a parser generator instead. Many have perfectly good RFC 2822 grammars.
For example, http://users.erols.com/blilly/mparse/rfc2822grammar_simplified.txt defines
addr-spec
which is the production you want, and you can define a grammatical production for a command, space, addr-spec, space, number and then define your top level production as a series of those separated by line-breaks.如果您使用空格作为分隔符,为什么不这样编码:
If you are using spaces as separator, why not code like this:
因此,如果您将数据放在名为
command
的变量中,您可以简单地执行以下操作:或者,您可以在字符串上使用“split”将其加载到数组中:
So if you have your data in a variable called
command
you could simply do:Alternatively, you can use "split" on the string to load it into an array:
在我看来,您的电子邮件地址确实已存储在您的
email
变量中。It looks to me like you do have the email address already stored in your
email
variable.