Extracting email address from error string

harishbabua8
Giga Contributor

Hello Everyone,

I need help in extracting email addresses from error string. The error string will look like this.

 

450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found

 

I tried different RegEx but no luck. Any help is appreciated.

3 ACCEPTED SOLUTIONS

AnveshKumar M
Tera Sage

Hi @harishbabua8 

 

You can use the following script to extract the email address.

 

var err_str = "450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found";
var invalid_recipient = "";
if (err_str_ln.indexOf("<") != -1 && err_str_ln.indexOf(">") != -1) {
   invalid_recipient = err_str.split('<')[1].split('>')[0];
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

 

View solution in original post

Yes,

This solution will work for you to extract all emails enclosed in < and >:

 

// Sample text containing values between < and >
var text = '450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found anotherEmail <example2@text.com>';

// Regular expression to extract values between < and >
var regex = /<([^>]*)>/g;

// Array to store extracted values
var extractedValues = [];

// Extract values using regex
var match;
while ((match = regex.exec(text)) !== null) {
  extractedValues.push(match[1]);
}

// Log the extracted values
gs.info('Extracted values:'+ extractedValues);

 

 

View solution in original post

@harishbabua8 

You can try the following script then.

 

var err_arr = err_str.split("\n");
var invalid_recipients = [];

for (key in err_arr) {
   var err_str_ln = err_arr[key];
   if (err_str_ln.indexOf("<") != -1) {
      invalid_recipients.push(err_str_ln.split('<')[1].split('>')[0]);
   }
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

View solution in original post

8 REPLIES 8

Anil Lande
Kilo Patron

Hi,

If your error message format is fixed then you can try below:

 

var str = "450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found";
var ind1 = str.indexOf('<');
var ind2 = str.indexOf('>');
gs.info(str.substring(ind1+1,ind2));

 

 

 

Anil Lande
Kilo Patron

If you want to use RegEx then try below:

// Sample text containing values between < and >
var text = '450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found';

// Regular expression to extract values between < and >
var regex = /<([^>]*)>/g;

// Array to store extracted values
var extractedValues = [];

// Extract values using regex
var match;
while ((match = regex.exec(text)) !== null) {
  extractedValues.push(match[1]);
}

// Log the extracted values
gs.info('Extracted values:'+ extractedValues);

Yes,

This solution will work for you to extract all emails enclosed in < and >:

 

// Sample text containing values between < and >
var text = '450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found anotherEmail <example2@text.com>';

// Regular expression to extract values between < and >
var regex = /<([^>]*)>/g;

// Array to store extracted values
var extractedValues = [];

// Extract values using regex
var match;
while ((match = regex.exec(text)) !== null) {
  extractedValues.push(match[1]);
}

// Log the extracted values
gs.info('Extracted values:'+ extractedValues);

 

 

Sohail Khilji
Kilo Patron

Hi @harishbabua8 ,

 

This will help :

SohailKhilji_0-1702370908622.png

 

 

 

var inputString = "450 4.1.2 <example@example.com>: Recipient address rejected: domain not found";
var emailRegex = /<([^>]+)>/;
var match = inputString.match(emailRegex);
var extractedEmail = match ? match[1] : null;

gs.info("Extracted Email: " + extractedEmail);