0

I have a html context that has usernames, mails and passwords in plain text format. There are two line breaks between information groups.

nickname
mail@example.com
pass


nickname2
mail2@example.com
pass2

I want to get all mails with a comma seperator and set them to a big string variable.

I tried many regexes but could not resolve the problem. Any suggestion to find the true regex will be appreciated.

Ece
  • 148
  • 9
  • 1
    Can you please post the regex(s) you already tried (no point in us re-doing work you already did)? If it a CSV you should be able to just loop through it and pull every second field value, no? – chris85 May 16 '16 at 19:54

2 Answers2

2

The pattern:

.+@.+.\.\w+

Example: https://regex101.com/r/nM1hK3/2

Edited

fdfey
  • 587
  • 2
  • 11
  • a@a is not an email address. a@a.a is. The correct pattern should be `".+@.+\..+"` – Nick May 16 '16 at 20:04
  • updated, thanks @Nick – fdfey May 16 '16 at 20:10
  • @fdfey it will probably not be any issue but since the list has both email and password, the password could be recognized as an email. `i.h@ve.a.passord` can be someones password but will be captured as email. One thing to do is to compare against TLD list http://data.iana.org/TLD/tlds-alpha-by-domain.txt but I doubt it will be a problem. I'm not saying your answer is incorrect, just that passord can be cought as email. – Andreas May 16 '16 at 20:41
  • That is true. This may work `.+\n(.+@.+.\.\w+)\n.+` instead of matching just the password it will match the whole block, but the password will be in the matching group. It also depends on whether or not your system uses \r\n or just \n for new lines. – Nick May 17 '16 at 15:06
0

If .txt file is not too big (if you have enough memory and time):

$lines = file('mails.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

function get_emails($val)
{
  if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
    return $val;
}
}
$emails=array_filter($lines,"get_emails");
//print_r($emails);

$emails_var=join(",",$emails);
//echo $emails_var;

'mails.txt' -> path to your file, of course.

sinisake
  • 11,240
  • 2
  • 19
  • 27