使用正则表达式和 Powershell 从字符串中提取特定数据 [英] Extracting specific data from a string with regex and Powershell

查看:25
本文介绍了使用正则表达式和 Powershell 从字符串中提取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个字符串中提取

I want to extract from this string

blocked-process-report process id="process435d948" taskpriority="0" logused="0" waitresource="RID: 7:1:1132932:0" waittime="3962166" ownerId="4641198" transactionname="SELECT" lasttranstarted="2011-09-13T17:21:54.950" XDES="0x80c5f060" lockMode="S" schedulerid="4" kpid="18444" status="susp结束" spid="58" sbid="0" ecid="0"

blocked-process-report process id="process435d948" taskpriority="0" logused="0" waitresource="RID: 7:1:1132932:0" waittime= "3962166" ownerId="4641198" transactionname="SELECT" lasttranstarted="2011-09-13T17:21:54.950" XDES="0x80c5f060" lockMode="S" schedulerid="4" kpid="18444" status="susp ended" spid="58" sbid="0" ecid="0"

粗体的值,但只有值或 58.这个值可以有不同的值,有时是 80 或 1000 等,但总是 > 50.

The value that is in bold, but only the value or 58. And this value can be with different values, sometimes 80 or 1000, etc. but always > 50.

我如何使用正则表达式和 posh 来做到这一点?

How can I do this using regex and posh?

推荐答案

又快又脏:

$found = $string -match '.*spid="(\d+)".*'
if ($found) {
    $spid = $matches[1]
}

其中 $string 是您上面提到的字符串.这将匹配任何具有 spid="somenumberhere" 的字符串,并将数字组合成一个匹配的组,您可以使用 $matches[1] 提取该组.

where $string is your above mentioned string. This would match any string that has spid="somenumberhere", and make the number into a matched group, which you can extract using $matches[1].

这篇关于使用正则表达式和 Powershell 从字符串中提取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆