Regex 在powershell中使用正则表达式搜索多个单词

Regex 在powershell中使用正则表达式搜索多个单词,regex,powershell,Regex,Powershell,我对powershell是新手。我非常感谢您在以下方面提供的任何帮助。我有一个powershell脚本,但无法完成从文本文件获取所有数据字段的操作 我有一个文件1.txt,如下所示 我试图从下面表格格式的文件中提取“pid”和“CTL00lblourPrice”的输出,以便在excel中打开它。列标题并不重要: pid ctl00仪表盘 0070362408美元6.70 008854787666美元50.70 目前我只能得到pid如下。还希望获得每个pid的价格。--> 0070362408 0

我对powershell是新手。我非常感谢您在以下方面提供的任何帮助。我有一个powershell脚本,但无法完成从文本文件获取所有数据字段的操作

我有一个文件1.txt,如下所示

我试图从下面表格格式的文件中提取“pid”和“CTL00lblourPrice”的输出,以便在excel中打开它。列标题并不重要:

pid ctl00仪表盘

0070362408美元6.70

008854787666美元50.70

目前我只能得到pid如下。还希望获得每个pid的价格。-->

0070362408

008854787666

c:\scan\1.txt:

This is sentence 1.. This is sentence 1.1... This is sentence A1...
fghfdkgjdfhgfkjghfdkghfdgh gifdgjkfdghdfjghfdg
gkjfdhgfdhgfdgh
ghfghfjgh
...
href='http://example.com/viewdetails.aspx?pid=0070362408'>
This is sentence B1.. This is sentence B2... This is sentence B3...
GFGFGHHGH
HHGHGFHG
<p class="price" style="display:inline;">
ctl00_lblOurPrice=$6.70
This is sentence 1.. This is sentence 1.1... This is sentence A1...
fghfdkgjdfhgfkjghfdkghfdgh gifdgjkfdghdfjghfdg
gkjfdhgfdhgfdgh
ghfghfjgh
...
href='http://example.com/viewdetails.aspx?pid=008854787666'>
This is sentence B1.. This is sentence B2... This is sentence B3...
6GBNGH;L
887656HGFHG
<p class="price" style="display:inline;">
ctl00_lblOurPrice=$50.70
...
...

提前感谢您的帮助

我假设您在代码中的意思是
pid=\d{1,13}
,或者您的示例文本应该是
num=
而不是
pid=
。我们将假设它实际上应该是
pid

在这种情况下,我们将使用
-Join”“
,将整个文件转换为一个长字符串,然后在“href”上拆分它,为每个要解析的站点创建记录。然后我们匹配pid=并在它遇到一个非数字字符时结束,然后我们查找一个美元金额(一个美元后跟数字,后跟一个句点,然后再加两个数字)

当我们有一对PID/Price匹配项时,我们可以创建一个具有两个属性的对象,PID和Price,并将其输出。为此,我将把它分配给一个数组,稍后使用。如果没有PSv3或更高版本,则必须将
[PSCustomObject][ordered]
更改为
新对象PSObject-Property
,但这会丢失属性的顺序,因此我更喜欢前者,并在这里的示例中使用它

$files=Get-ChildItem C:\scan -recurse
$output_file = 'c:\output\outdata.csv'
$Results = @()
foreach ($file in $files) {
    $Results += ((gc $File) -join "") -split "href" |?{$_ -match "pid=(\d+?)[^\d].*?(\$\d*?\.\d{2})"}|%{[PSCustomObject][ordered]@{"PID"=$Matches[1];"Price"=$Matches[2]}}
}

$Results | Select PID,Price | Export-Csv $output_file -NoTypeInformation

如果该字符串是示例数据,则它与代码完全不一致。在我能看到的任何地方都没有'num=\d{1,13}'(或任何'num='子字符串)。
$files=Get-ChildItem C:\scan -recurse
$output_file = 'c:\output\outdata.csv'
$Results = @()
foreach ($file in $files) {
    $Results += ((gc $File) -join "") -split "href" |?{$_ -match "pid=(\d+?)[^\d].*?(\$\d*?\.\d{2})"}|%{[PSCustomObject][ordered]@{"PID"=$Matches[1];"Price"=$Matches[2]}}
}

$Results | Select PID,Price | Export-Csv $output_file -NoTypeInformation