免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5370 | 回复: 2
打印 上一主题 下一主题

求问perl 一个分割字符串 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-21 17:05 |只看该作者 |倒序浏览
我的字符串是如下形式:

PPP adapterr:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2035::1
   Link-local IPv6 Address . . . . . : fe80::1%25
   IPv4 Address. . . . . . . . . . . : 172.16.1.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.255

Ethernet adapter Local Area Connection 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::28fd:566a:9fce:27b1%14
   IPv4 Address. . . . . . . . . . . : 13.13.13.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection:

   IPv4 Address. . . . . . . . . . . : 10.10.10.1
   Subnet Mask . . . . . . . . . . . : 255.255.0.0

Tunnel adapter isatap.{378C8BD2-4DBC-4B71-A632-C3E967087D3B}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.{F1918DC9-09D2-4827-980B-A1A217223E49}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter Teredo Tunneling Pseudo-Interface:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter isatap.{CBBB79EB-3416-43EB-AFF3-7CC11E5F8AEA}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :


按照每个adapter来分割字符串,得到的一个数组,数组每个元素的值一个是一个adapter的内容。
如数组第0个元素为
PPP adapter:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2035::1
   Link-local IPv6 Address . . . . . : fe80::1%25
   IPv4 Address. . . . . . . . . . . : 172.16.1.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.255

第一个元素为
Ethernet adapter Local Area Connection 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::28fd:566a:9fce:27b1%14
   IPv4 Address. . . . . . . . . . . : 13.13.13.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

我想到的是
  1. my @results = split /(?=(?:PPP|Ethernet|Tunnel) adapter)/i, $result;
复制代码
$result 为上面字符串, @results为分割字符串得到的数组。
我想问的是有没有更简洁的写法?可以不用写出(?P|Ethernet|Tunnel),但用
  1. my @results = split /(?= adapter)/i, $result;
复制代码
前面的PPP, Ethernet, Tunnel 就会被截断了。

论坛徽章:
0
2 [报告]
发表于 2013-08-21 18:11 |只看该作者
  1. my @result = split /^(?=\S)/m, $result;
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-08-22 10:50 |只看该作者
回复 2# cinanine
谢谢,我试了,你的写法的确更简单方便。

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP