2012-11-23 1 views
2

단일 정규식 줄 사용 (응용 프로그램의 구성 파일에 있음) 전자 메일 주소의 localpart를 캡처해야합니다.문자열을 PCRE에서 크기를 줄입니다

숫자로만 구성된 경우 수정되지 않은 채로 전달하십시오. 숫자가 아닌 문자가 있으면 최대 11자를 잘라냅니다.

이 간단한 프로그램을 테스트했지만 localpart가 11자를 넘으면 일치하는 것이 없습니다 (전자 메일 주소 전체가 인쇄 됨).

#!/usr/bin/env perl 

my @emails = ('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'); 

for my $email (@emails){ 

    # will put $1$2 on the substituion spot 
    $email =~ s/^(\d+)@.+|^([a-zA-Z0-9._%+-]{1,11})@.+/ /; 

    print '===> ' . $email . " \n\n "; 

} 

나는 어디로 가고 있습니까?

답변

2

당신이 올바른 출력을 생성하여 정규식 원자 문자 수가 11 초과하는 경우에는 다음과 같은 \S*? ...

$email =~ s/^(\d+)@.+|^([a-zA-Z0-9._%+-]{1,11})\S*[email protected]+/$1$2/; 

을 포함해야합니다

[[email protected] ~]$ perl sample.pl 
===> the-chuck.t 

===> 358451399991 

===> ph33t 

===> the-average 

===> alongnameit 

===> alongnameit 

===> a1234567890 

[[email protected] ~]$ 
+0

누군가가 그것을 보여주고 난 후에 분명 ... :) 감사! –