2017-12-02 3 views
-1

하나 또는 두 개의 전자 메일 주소가 쉼표로 구분 된 스칼라 (테이블의 열)가 있습니다. 나쁜/오래된 이메일 주소와 뒤에 오는 쉼표 (예 : )를 제거하는 데 필요한 이들 기록 중 일부는 '[email protected], [email protected]'또는 'jsmith @ wellingent.com, mjones @ wellingent.com'과 같은 존재하는 경우).perl 정규 표현식에 스칼라와 구두점이 일치합니다.

jmsith @ wellingent가 더 이상 유효하지 않은 경우 어떻게 그 주소와 후행 쉼표를 제거합니까?

주소 만 제거하지만 쉼표는 사용하지 않습니다.

my $general_email = '[email protected],[email protected]'; 
my $bad_addr = '[email protected]'; 

$general_email =~ s/$bad_addr//; 

도움 주셔서 감사합니다.

답변

1

당신은 정규식하지 않고 있지만 목록 분할과 더 좋을 수 있습니다

use strict; 
use warnings; 

sub remove_bad { 
    my ($full, $bad) = @_; 
    my @emails = split /\s*,\s*/, $full; # split at comma, allowing for spaces around the comma 
    my @filtered = grep { $_ ne $bad } @emails; 
    return join ",", @filtered; 
} 

print 'First: ' , remove_bad('[email protected], [email protected]', '[email protected]'), "\n"; 
print 'Last: ', remove_bad('[email protected], [email protected]', '[email protected]'), "\n"; 
print 'Middle: ', remove_bad('[email protected], [email protected], [email protected]', '[email protected]'), "\n"; 

첫째, split 배열을 만드는 쉼표의 잘못된 이메일 주소 목록. grep을 사용하여 필터링하여 잘못된 주소를 제거하십시오. join 나머지 요소를 문자열로 되돌립니다.

위의 코드를 인쇄 :

첫째 : [email protected]

마지막 : [email protected]

중학교 : 나 @ example.org, 기타 @ eample.org

+0

위양성을 피하기 위해'! m/$ bad /'를'$ _ ne $ bad '로 바꾸 겠지만 가장 강력한 솔루션 인 것 외에도. – PerlDuck

+0

@ PerlDuck 좋은 지적, 감사합니다! 결정된. – Robert

관련 문제