littleK
littleK

Reputation: 20163

Java parsing many instances of substring from String

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!

Upvotes: 1

Views: 2198

Answers (3)

Balamurugan
Balamurugan

Reputation: 11

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503419

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]".

Upvotes: 1

Brendan Long
Brendan Long

Reputation: 54312

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;
}

Upvotes: 2

Related Questions