powershell 字符串处理提取

常用到的有三种:

1、 -match 运算符,通过 $Matches来获取字段。

比如我之前的或取ip地址:http://hi.baidu.com/shrekzz/item/1bcf45ffc04d461fff3582fc

$WebClient=new-object System.Net.WebClient
$url="http://iframe.ip138.com/ic.asp"
$WebClient.DownloadString($url) -match '(?<ip>(\d+\.){3}\d+)'|out-null
$Matches.ip

日志入库中的方法:http://hi.baidu.com/shrekzz/item/4add3720c11b790143634a21

$cname|%{$_ -match 'ERROR...(?<time>.+):\d+  区ID = (?<zoon>\d+) , 组ID = (?<group>\d+) , 线ID = (?<line>\d+) , 人数 = (?<num>\d+)'
$logtime=$Matches.time
$logtype=1902
$regionid=$Matches.zoon
$groupid=$Matches.group
$lineid=$Matches.line
$online_role_count=$Matches.num
}   

2、matches()方法获取字段。

$txt='aaaa ssd <112@baidu.com>;
bbs sdg  wwe <22888@baidu.com>;
333p998 44iiiiiiii<aab@baidu.com>;
33 341 <44@baidu.com>;'
[regex]::matches($txt, '<(.+)>;') | %{$_.Groups[1].Value}

3、-split方法

("we-rt-192.168.12.11-45.exe" -split "-")[2]


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值