powershell regex从csv文件中的列替换字符串

时间:2018-04-23 18:07:53

标签: regex powershell csv

我需要从powershell中的csv文件中提取值。 这是CSV文件的示例:

"1","ANYDATA-481190-131","<ul>
<li><strong>487410-131</strong> (prateado)</li>
<li><strong>502478-131</strong></li>
<li><strong>555996-131</strong> (prateado)</li>
<li><strong>AEQT6T11110</strong></li>
<li><strong>AEQT6T00210</strong></li>
</ul>"

我需要在标记<strong></strong>之间提取值,并用逗号分隔,在这样的新CSV文件中:

"1","ANYDATA-481190-131","487410-131, 502478-131, 555996-131, AEQT6T11110, AEQT6T00210"

编辑:

以下是未来搜索的答案:

import-csv $CSV -delimiter "," -Header 1,2,3 | Select '1','2',@{n='3';e={[regex]::Matches(($_.3).Split("`n"),"(?<=<strong>)(.*?)(?=</strong>)").Value -join ", "}} | Export-csv $CSV2 -Encoding UTF8 -Notype

对于名为1,2和3的3列,其中$ CSV是原始CSV文件,$ CSV2是目标文件,

1 个答案:

答案 0 :(得分:0)

可能没有正则表达式吗?

ConvertFrom-Csv @'
"1", "ANYDATA-481190-131", "<ul>
<li><strong>487410-131</strong> (prateado)</li>
<li><strong>502478-131</strong></li>
<li><strong>555996-131</strong> (prateado)</li>
<li><strong>AEQT6T11110</strong></li>
<li><strong>AEQT6T00210</strong></li>
</ul>"
'@ -Header 1, 2, 3 -Delimiter ','|
  Select-Object '1', '2', @{
    n='3'
    e={
      $ofs=','
      """$(
        [xml]$_.3|
          Select-Object -ExpandProperty ul|
          Select-Object -ExpandProperty li|
          Select-Object -ExpandProperty strong
      )"""
    }
  }

1 2                  3                                                       
- -                  -                                                       
1 ANYDATA-481190-131 "487410-131,502478-131,555996-131,AEQT6T11110,AEQT6T00210"

Try it online!