在批处理或 PoSh 的引号之间删除逗号 [英] Remove comma when between quotes from batch or PoSh

查看:42
本文介绍了在批处理或 PoSh 的引号之间删除逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的 CSV 文件:

A,B,CD,"E,F",GH,I,"J,K,L"

我需要删除引号之间的逗号(也删除引号,但这并不重要):

A,B,CD,EF,GH,I,JKL

我查看了 PoSh

正则表达式是 "([^"]*)" 匹配 ",然后捕获到组 1(即 $match.Groups[1].Value) 除 " 之外的任何零个或多个字符,然后匹配 ".如果您对引号进行了转义,则需要增强,但方法是相同的.

I have a CSV file with a content like:

A,B,C
D,"E,F",G
H,I,"J,K,L"

I need to remove the commas when between quotes (also remove the quotes, but that is not so important):

A,B,C
D,EF,G
H,I,JKL

I looked at PoSh -replace operator but I can't get it to capture multiple group values:

 PS >"D,`"E,F`",G" -replace "`"((?:[^,`"]+)\,?)+`"", '$1'
 D,F,G

as you can see when the group is repeated, only the last value captured is preserved. Is there a way to do the transformation I want?

https://regex101.com/r/ON1rgp/1/

解决方案

You may define a callback to pass to the Regex::Replace method where you may just grab the part between quotes and remove all , there:

$callback = {  param($match) $match.Groups[1].Value.Replace(',','') }
$s =  "D,`"E,F`",G"
$rex = [regex]'"([^"]*)"'
$rex.Replace($s, $callback)

The regex is "([^"]*)" that matches ", then captures into Group 1 (i.e. the $match.Groups[1].Value) any zero or more chars other than " and then matches ". It will need enhancing in case you have escaped quotes, but the approach will be the same.

这篇关于在批处理或 PoSh 的引号之间删除逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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