-1

Need help on Regular Expression to retrieve Email IDs in Error Stack which is like "Some Text some text line break and so on".

Tried using some suggestions provided in Stack overflow. But most of them prints just the error

Below are some of the options which I tried out,

Matcher m = Pattern.compile("\\<([^>]+)\\)").matcher(e.getMessage());
while(m.find())
{
    System.out.println(m.group(1));
}
System.out.println(e.getMessage().split("<(<^>>+)>"));

exception.getMessage().split("\\[([^]]+)\\]")
exception.getMessage().split("\\<\"(.*?)\"\\>")
exception.getMessage().split("<(<^>>+)>")

Actual Resulting String array contains only one value and prints the entire stack as below,

Failed messages: javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.0.0 <abc@def.com>... User unknown
;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.0.0 <def@ghi.com>... User unknown
;

PS: There is no \ in the email id. Stack overflow did not accept the character followed by <. So added escape character before that.

Giftson David
  • 23
  • 1
  • 9
  • Why are you attempting to use split and regex? Split returns an array of Strings, and the param you pass to it is what it removes on. – adickinson Apr 09 '19 at 14:52
  • @adickinson I am actually very new to this. Yes, using split was not proper. So changed to use matcher which worked fine for a different RegEx. Will post the answer now. Thanks.! – Giftson David Apr 09 '19 at 15:07

2 Answers2

0

This should do what you want, albeit with the assumption that there will only ever be one per message.

public class someClass {
    private static final Pattern idPattern = Pattern.compile("<.*>");

    public static void main(String[] args) {
        doStuff();
    }

    public static void doStuff(){
        try{
            throwNPE();
        } catch (NullPointerException ex){
            String message = ex.getMessage();
            if(idPattern.matcher(message).matches()){
                System.out.println(getId(message));
            }
        }
    }

    public static String getId(String message){
        String[] messageParts = message.split("<|>");
        if(messageParts.length > 1){
            return messageParts[1];
        }else{
            throw new IllegalArgumentException();
        }
    }

    public static void throwNPE(){
        throw new NullPointerException("<123>");
    }
}

You only really need doStuff() and getId(String) for your example (and you can drop the static), however wanted to provide a working example.

Any explanations necessary, please ask. Good luck!

adickinson
  • 553
  • 1
  • 3
  • 14
  • Thanks for the suggestion. There will be many instances of the ID and I actually needed something simple. Please suggest whether the answer I posted will do fine. – Giftson David Apr 09 '19 at 15:19
0

This works fine as per the accepted answer in, Regular expression to extract text between square brackets

Pattern.compile("\\<(.*?)\\>").matcher(e.getMessage());
Giftson David
  • 23
  • 1
  • 9