将电子邮件地址读取为令牌

发布于 2024-12-17 13:46:10 字数 511 浏览 3 评论 0原文

如何读取电子邮件地址作为令牌?

我看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-12-24 13:46:10

StringTokenizer 不是完成这里任务的工具。电子邮件对于它来说太复杂了,无法处理,因为它无法将本地部分是带引号字符串的有效电子邮件地址视为一个标记:

"foo bar"@example.com

请改用解析器生成器。许多都有非常好的 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:

"foo bar"@example.com

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.

一直在等你来 2024-12-24 13:46:10

如果您使用空格作为分隔符,为什么不这样编码:

String[] temp =command.split(" ");
String email = temp[1];
String amount = temp[2];

If you are using spaces as separator, why not code like this:

String[] temp =command.split(" ");
String email = temp[1];
String amount = temp[2];
欲拥i 2024-12-24 13:46:10

因此,如果您将数据放在名为 command 的变量中,您可以简单地执行以下操作:

StringTokenizer st = new StringTokenizer(command);
st.nextToken(); //discard the "command" token since you don't care about it
String email = st.nextToken();
String amount = st.nextToken();

或者,您可以在字符串上使用“split”将其加载到数组中:

String[] tokens = command.split("\w"); //this splits on any whitespace, not just the space
String email = tokens[1];
String amount = tokens[2];

So if you have your data in a variable called command you could simply do:

StringTokenizer st = new StringTokenizer(command);
st.nextToken(); //discard the "command" token since you don't care about it
String email = st.nextToken();
String amount = st.nextToken();

Alternatively, you can use "split" on the string to load it into an array:

String[] tokens = command.split("\w"); //this splits on any whitespace, not just the space
String email = tokens[1];
String amount = tokens[2];
神爱温柔 2024-12-24 13:46:10

在我看来,您的电子邮件地址确实已存储在您的 email 变量中。

package com.so;

import java.util.StringTokenizer;

public class Q8228124 {
    public static void main(String... args) {
        String input = "command [email protected] 50";

        StringTokenizer tokens = new StringTokenizer(input);

        System.out.println(tokens.countTokens());

        // Your code starts here.
        String test2 = input.substring(7);
        StringTokenizer st = new StringTokenizer(test2);
        String email = st.nextToken();
        String amount = st.nextToken();

        System.out.println(email);
        System.out.println(amount);
    }
}

$ java com.so.Q8228124
3
[email protected]
50

It looks to me like you do have the email address already stored in your email variable.

package com.so;

import java.util.StringTokenizer;

public class Q8228124 {
    public static void main(String... args) {
        String input = "command [email protected] 50";

        StringTokenizer tokens = new StringTokenizer(input);

        System.out.println(tokens.countTokens());

        // Your code starts here.
        String test2 = input.substring(7);
        StringTokenizer st = new StringTokenizer(test2);
        String email = st.nextToken();
        String amount = st.nextToken();

        System.out.println(email);
        System.out.println(amount);
    }
}

$ java com.so.Q8228124
3
[email protected]
50
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文