-3

For example I have this string, where email addresses are differently formatted:

"a.b@c.d" <a.b@c.d>|"Michal pichal (michal.pichal@g.d)" <michal.pichal@g.d>|Melisko Pan <melisko.pan@domena.sk>

I need to extract the email addresses in a form of:

a.b@c.d|michal.pichal@g.d|melisko.pan@domena.sk

My idea was to get any char near @ from group [0-9][A-Z][a-z]@.- but I do not know how. Please help with some hint.

tnw
  • 13,521
  • 15
  • 70
  • 111
  • Have you tried anything? You ask for a hint but you've already tagged regex. That's my hint. Go do some research and look up a regex for email addresses and try writing some code. A cursory search revealed this duplicate: http://stackoverflow.com/questions/16053797/regex-to-find-email-address-from-a-string -1 for obvious lack of research and effort. – tnw Apr 12 '17 at 16:25
  • You don't need a regex for this. Just get everything between `<` and `>`. Concatenate with pipe characters and you're done. – Michael Apr 12 '17 at 16:28
  • Can you please not post what seems to be real email adresses? –  Apr 12 '17 at 16:30
  • Unless, of course, some of the e-mails are not in that format. – RealSkeptic Apr 12 '17 at 16:30
  • You can do it in Java Pattern compiling. First you extract everything between < and > and then concatnate it like, Michael mentioned. Here it's mentioned: http://stackoverflow.com/questions/6560672/java-regex-to-extract-text-between-tags – Om Sao Apr 12 '17 at 16:31
  • @OmSao "Java Pattern compiling" is regex. I think Michael's point was that regex was not needed. – RealSkeptic Apr 12 '17 at 16:32

2 Answers2

0
import java.util.Scanner;
class test{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String res = ""; //This holds the final result
        for(int i=0; i<str.length(); ++i){ //Loop through the entire string
            if(str.charAt(i)=='<'){
                String build = "";
                for(int j=i+1; j<str.length(); ++j){
                    if(str.charAt(j)=='>'){
                        break; //Break when we hit the '>'
                    }
                    build += str.charAt(j); //Add those between < and >
                }
                res += build + "|"; //Add the pipe at the end
            }
            continue;
        }
        System.out.println(res);
    }
}

This ought to do it. Just run simple nested loops. No need for regex.

Hans
  • 1
  • 3
0

This regex extracts emails out of your string:

public static void main(String[] args) {
        Pattern pattern = Pattern.compile("[\\w.]+@[\\w.]+");
        Matcher matcher = pattern.matcher("\"a.b@c.d\" <a.b@c.d>|\"Michal pichal (michal.pichal@g.d)\" <michal.pichal@g.d>|Melisko Pan <melisko.pan@domena.sk>\r\n");
        while(matcher.find()){
            String group = matcher.group();
            System.out.println("group="+group);
        }

It prints:

group=a.b@c.d
group=a.b@c.d
group=michal.pichal@g.d
group=michal.pichal@g.d
group=melisko.pan@domena.sk
Jay Smith
  • 2,331
  • 3
  • 16
  • 27