Java从字符串中解析子字符串的许多实例

发布于 2024-08-20 00:35:28 字数 229 浏览 3 评论 0原文

我正在尝试编写一个小型java程序,它将接受一个文件(使用Scanner类),将文件作为字符串返回,然后在该字符串中搜索以“Email:”开头并以“.edu”结尾的子字符串的任何实例”。该子字符串会有很多实例,我想将每个实例解析为一个数组或一个新文件。

我知道如何查找子字符串,但我不知道如何 A) 搜索子字符串的所有实例和 B) 指定子字符串的开始和结束。

有人可以帮我解决这个逻辑吗?

谢谢!

I am trying to write a small java program that will accept a file (using Scanner class), return the file as a String, and then search that string for any instance of a substring starting with "Email:" and ending with ".edu". There will be many instances of this substring, each of which I want to parse out into an array or a new file.

I know how to find a substring, but I do not know how to A) search for all instances of the substring and B) specify the start AND finish of the substring.

Can someone help me with this logic?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

樱花落人离去 2024-08-27 00:35:28

您可以使用indexOf()。我想你也可以告诉它从哪里搜索。因此,要查找“Email:”的实例:

while(index < input.size()){
  substringLocation = input.indexOf("Email:", index);
  // do something with substring
  index = substringLocation;
}

You could use indexOf(). I think you can tell it where to search from too. So to find your instances of "Email:":

while(index < input.size()){
  substringLocation = input.indexOf("Email:", index);
  // do something with substring
  index = substringLocation;
}
请止步禁区 2024-08-27 00:35:28

对我来说,这听起来像是正则表达式的情况:

import java.util.regex.*;

public class Test
{
    private static final Pattern EMAIL_PATTERN = Pattern.compile
        ("Email:(.*?\\.edu)");

    public static void main(String[] args)
    {
        String testString = "FooEmail:[email protected] Bar Email:[email protected] Baz";

        printEmails(testString);
    }

    public static void printEmails(String input)
    {
        Matcher matcher = EMAIL_PATTERN.matcher(input);
        while (matcher.find())
        {
            System.out.println(matcher.group(1));
        }
    }
}

请注意,如果其中有任何 .edu 电子邮件,您会得到奇怪的结果...例如,如果您有“电子邮件:[email protected] 电子邮件:[email protected]" 你最终会得到“[电子邮件受保护] 电子邮件:[电子邮件受保护]"。

This sounds like a case for regular expressions to me:

import java.util.regex.*;

public class Test
{
    private static final Pattern EMAIL_PATTERN = Pattern.compile
        ("Email:(.*?\\.edu)");

    public static void main(String[] args)
    {
        String testString = "FooEmail:[email protected] Bar Email:[email protected] Baz";

        printEmails(testString);
    }

    public static void printEmails(String input)
    {
        Matcher matcher = EMAIL_PATTERN.matcher(input);
        while (matcher.find())
        {
            System.out.println(matcher.group(1));
        }
    }
}

Note that you'll get strange results if you have any non .edu emails in there... for example, if you have "Email: [email protected] Email: [email protected]" you'd end up with a match of "[email protected] Email: [email protected]".

沩ん囻菔务 2024-08-27 00:35:28
private static final Pattern EMAIL_PATTERN = Pattern.compile
    ("Email:(.*?\\.[a-z]*?[\\.[a-z]]*)"); 

将解决该问题,并且 itt 适用于任何电子邮件模式,例如 xyz.com 中的 abc.co. 或 test.fileserver.abc.co.bz 域。

private static final Pattern EMAIL_PATTERN = Pattern.compile
    ("Email:(.*?\\.[a-z]*?[\\.[a-z]]*)"); 

Will solve the problem and itt will work for any email pattern such as abc.co.in xyz.com or test.fileserver.abc.co.bz domains.

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