Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式替换PowerShell字符串_Regex_String_Powershell_Replace - Fatal编程技术网

Regex 使用正则表达式替换PowerShell字符串

Regex 使用正则表达式替换PowerShell字符串,regex,string,powershell,replace,Regex,String,Powershell,Replace,这是一个正则表达式问题,而不是PS问题。下面是: 我有一个包含如下数据的文本文件 ABC Corp, x567 xyz Corp, y567 pqr Corp, m567 ghysds ,inc, x567 TRWsdsdsds ,org, y567 TYUds ,ing, m567 如何删除第4-6行的第一个逗号?(这些行有两个逗号。我只需要第二个。)我的计划是将这些数据插入一个有两列的表中 多谢各位 您必须使用“向前看”来检查行上是否有第二个逗号 ,(?=.*,) 使用此选项将

这是一个正则表达式问题,而不是PS问题。下面是:

我有一个包含如下数据的文本文件

ABC Corp, x567 
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
如何删除第4-6行的第一个逗号?(这些行有两个逗号。我只需要第二个。)我的计划是将这些数据插入一个有两列的表中


多谢各位

您必须使用“向前看”来检查行上是否有第二个逗号

,(?=.*,)

使用此选项将匹配的内容替换为空字符串。这将删除其中包含两个逗号的行的第一个逗号。

您必须使用“向前看”来检查行上是否有第二个逗号

,(?=.*,)

使用此选项将匹配的内容替换为空字符串。这将删除其中包含两个逗号的行的第一个逗号。

您必须使用“向前看”来检查行上是否有第二个逗号

,(?=.*,)

使用此选项将匹配的内容替换为空字符串。这将删除其中包含两个逗号的行的第一个逗号。

您必须使用“向前看”来检查行上是否有第二个逗号

,(?=.*,)

使用此选项将匹配的内容替换为空字符串。这将去掉第一个逗号,因为其中有两个逗号。

这并不需要正则表达式,尽管它可以工作

$StringList = @('abd,asdc,asdc', 'asdc,awegwe,aweg', 'asdfasdf,asdaweg');

foreach ($String in $StringList) {
    if ($String -match '.*,.*,') {
        $String.Remove($String.IndexOf(','), 1);
    }
    else {
        $String;
    }
}

虽然正则表达式可以工作,但实际上并不需要正则表达式

$StringList = @('abd,asdc,asdc', 'asdc,awegwe,aweg', 'asdfasdf,asdaweg');

foreach ($String in $StringList) {
    if ($String -match '.*,.*,') {
        $String.Remove($String.IndexOf(','), 1);
    }
    else {
        $String;
    }
}

虽然正则表达式可以工作,但实际上并不需要正则表达式

$StringList = @('abd,asdc,asdc', 'asdc,awegwe,aweg', 'asdfasdf,asdaweg');

foreach ($String in $StringList) {
    if ($String -match '.*,.*,') {
        $String.Remove($String.IndexOf(','), 1);
    }
    else {
        $String;
    }
}

虽然正则表达式可以工作,但实际上并不需要正则表达式

$StringList = @('abd,asdc,asdc', 'asdc,awegwe,aweg', 'asdfasdf,asdaweg');

foreach ($String in $StringList) {
    if ($String -match '.*,.*,') {
        $String.Remove($String.IndexOf(','), 1);
    }
    else {
        $String;
    }
}
这是我的:

$text = 
(@'
ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
'@).split("`n")

$text -replace '(.+?),(.+?),(.+)','$1$2,$3'

ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds inc, x567 
TRWsdsdsds org, y567 
TYUds ing, m567
这是我的:

$text = 
(@'
ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
'@).split("`n")

$text -replace '(.+?),(.+?),(.+)','$1$2,$3'

ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds inc, x567 
TRWsdsdsds org, y567 
TYUds ing, m567
这是我的:

$text = 
(@'
ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
'@).split("`n")

$text -replace '(.+?),(.+?),(.+)','$1$2,$3'

ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds inc, x567 
TRWsdsdsds org, y567 
TYUds ing, m567
这是我的:

$text = 
(@'
ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds ,inc, x567 
TRWsdsdsds ,org, y567 
TYUds ,ing, m567
'@).split("`n")

$text -replace '(.+?),(.+?),(.+)','$1$2,$3'

ABC Corp, x567
xyz Corp, y567 
pqr Corp, m567 
ghysds inc, x567 
TRWsdsdsds org, y567 
TYUds ing, m567

你可以试试这个:第二个答案是你想要的。你可以试试这个:第二个答案是你想要的。你可以试试这个:第二个答案是你想要的。你可以试试这个:第二个答案是你想要的。这是最优雅的方式。谢谢你!回答得很好。不幸的是,作为一个新手,我不能推广你的答案。这是最优雅的方式。谢谢你!回答得很好。不幸的是,作为一个新手,我不能推广你的答案。这是最优雅的方式。谢谢你!回答得很好。不幸的是,作为一个新手,我不能推广你的答案。这是最优雅的方式。谢谢你!回答得很好。不幸的是,作为一个新手,我不能推广你的答案。