匹配电子邮件地址与特殊字符

时间:2015-08-14 06:07:11

标签: regex perl

我有一些.yml文件,其中一些包含其中的电子邮件地址以及其他数据。我试图查找包含任何这些字符的电子邮件地址!@#$%^&*()_-+{}

例如chankey!007@testserver.com。这些可能是也可能不是无效的地址。我只需要在包含上述特殊字符的@之前获取数据。我尝试了以下但没有得到预期的结果。

#!/usr/bin/perl
use warnings;
use strict;
use File::Find;

open (my $out, ">", 'output.txt') or die $!;

find(sub {
  if (-f and /\.yml$/) {
    my $data;
    {
      local $/=undef;
      open my $file, "<", $_ or die "Couldn't open file: $!";
      $data = <$file>;
      close $file;
    }
    if ($data =~ /([a-zA-z0-9+.$#_-]+)@/g){
        my $data = $1;
        if ($data =~ /[+.$#_-]/g){
            print $out "File: $_\n";        
            print $out $data."\n";
        }

    }
  }
}, '.');

2 个答案:

答案 0 :(得分:0)

(^.*[!@#$%^&*()_\-+{}]+.*)@

只有当包含您提到的任何特殊字符时,才会捕获用户名。您必须添加\g修饰符才能重复匹配。以下是regex101.com的细分:

1st Capturing group (^.*[!@#$%^&*()_\-+{}]+.*)
    ^ assert position at start of the string
    .* matches any character (except newline)
        Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    [!@#$%^&*()_\-+{}]+ match a single character present in the list below
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
        !@#$%^&*()_ a single character in the list !@#$%^&*()_ literally (case sensitive)
        \- matches the character - literally
        +{} a single character in the list +{} literally
    .* matches any character (except newline)
        Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
@ matches the character @ literally
g modifier: global. All matches (don't return on first match)

答案 1 :(得分:0)

<强> EDITED

将我的正则表达式更新为只捕获那些包含类中字符的地址

根据您在regex101链接上提供的数据类型,此类内容可能有效

(\w*[-!@#$%^&*()_+{}]+\w*)@

仅供参考,通过将-作为字符类中的第一个字符,正则表达式引擎将其视为文字字符,而不是字符类范围操作符。此外,下划线已在\w中考虑,但我将其留下以使正则表达式更明确。

此外,但使用\w而不是.+.*,这意味着我们无法匹配任何空白,无论如何都不应该在电子邮件地址中。