perl 自定义包模块的使用(正则表达式匹配email地址和ip)

perl 自定义包模块的使用(正则表达式匹配email地址和ip),第1张

概述1. 正则表达式 email: =~ /^([0-9a-zA-Z]{3,}|[0-9a-zA-Z]+(-|_)?[0-9a-zA-Z_-]+\.?[0-9a-zA-Z]+)\@[0-9a-zA-Z]+\.[0-9a-zA-Z]+(\.\w+)?/; ip =~/([0-9]{1,3}\.?){4}/;   . Match any character except a wrap character.

1. 正则表达式

email: =~ /^([0-9a-zA-Z]{3,}|[0-9a-zA-Z]+(-|_)?[0-9a-zA-Z_-]+\.?[0-9a-zA-Z]+)\@[0-9a-zA-Z]+\.[0-9a-zA-Z]+(\.\w+)?/;

ip =~/([0-9]{1,3}\.?){4}/;

 

. Match any character except a wrap character.

\w Match "word" character and [_0-9a-zA-Z]

\W Match non-word character  [^_0-9a-zA-Z]

\s  Match whitespace character [ \r\t\n\f]

\S Match non-whitespace character [^ \r\t\n\f]

\d  Match digit character [0-9]

\D Match non-digit character [^0-9]

\t  Match tab

\n Match newline

^ Match the head

$ Match the end

*  Match 0 or more times

+  Match 1 or more times

?  Match 0 or 1 times

{n} Match exactly n times

{n,} Match at least n times

 {n,m} Match at least n but not more than m times

a|b  match a or match b

2.自定义包/模块

包和模块的区别:

1.一个包的定义可以跨多个模块,一个模块中也可以有多个包定义;

2.定义模块时模块名必须和文件名一致,包无此要求;

注意:包和模块文件末尾需要加1;(return 1)

 

比如: 定义包名为getip.pm 在包文件里写入package getip;

VIEw Code  #!/usr/bin/perl -w package packagetest; use strict; sub get_local_ip {   chomp(my $ip=`ifconfig eth0|grep -oE '([0-9]{1,3}\.?){4}'|head -n 1`);   return $ip; } sub check_email {   my $temp=$_[0];   if($temp=~/^([0-9a-zA-Z]{3,}|[0-9a-zA-Z]+(-|_)?[0-9a-zA-Z_-]+\.?[0-9a-zA-Z]+)\@[0-9a-zA-Z]+\.[0-9a-zA-Z]+(\.\w+)?/)   {    return $temp;   }   else   {     return $temp="illegal email address!"   } } 1;


 

3.如何使用

使用自定义包,在perl文件加入

package packagetest;(和包放在同一目录下)

使用函数:

包名::函数名

VIEw Code  #!/usr/bin/perl -w require "package.pm"; package packagetest; $localip=&packagetest::get_local_ip; print $localip; print "\n"; $mail=&packagetest::check_email('someone_example@cn.com'); print "$mail\n"; [root@localhost perl]# more getip.pl  #!/usr/bin/perl -w require "package.pm"; package packagetest; $localip=&packagetest::get_local_ip; print $localip; print "\n"; $mail=&packagetest::check_email('someone_example@cn.com'); print "$mail\n";


 

总结:

1. perl很开放,正则表达式也很深奥,很多情况perl不报错但是结果是错的。

2.perl模块很多,有些模块依赖别的模块,需要一一安装。

总结

以上是内存溢出为你收集整理的perl 自定义包/模块的使用(正则表达式匹配email地址和ip)全部内容,希望文章能够帮你解决perl 自定义包/模块的使用(正则表达式匹配email地址和ip)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1291649.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存