【问题标题】:How do I replace the delimiter in a series of paths that can contain that delimiter?如何在可以包含该分隔符的一系列路径中替换分隔符?
【发布时间】:2019-06-04 21:45:51
【问题描述】:

如果我有字符串:

path1=/path/me & you/file.json&path2=/path/you & me/file.txt

我希望输出是这样的:

path1=/path/me & you/file.json;path2=/path/you & me/file.txt

我试过这样:

"path1=/path/me & you/file.json&path2=/path/you & me/file.txt" -replace "&",";"

但它会产生

path1=/path/me ; you/file.json;path2=/path/you ; me/file.txt

【问题讨论】:

  • 到目前为止,您是否尝试过自己做任何事情?如果您不尝试以尽可能少的字节来解决它,这看起来是一个可以解决的问题。只需使用-split "&",然后根据需要将各个部分连接在一起
  • 具体条件是什么?如果您能描述您要解决的一般问题,那将会很有帮助。也就是说,假设您不仅要处理path1=/path/me & you/file.json&path2=/path/you & me/file.txt,而且要处理任何带有pathx 值的string,这些值由& 分隔并且它们本身可能包含&,是否安全?会一直有path1path2,或者有时更多,有时更少?
  • @BACON 是的会有另外一个path2、path3等可能性:path1=/me & you/file.json&path2=/folder/R&B/me & you/file.txt

标签: regex string powershell replace delimiter


【解决方案1】:

当然,问题在于,有时& 分隔 路径,有时它是路径的一部分。一个简单的字符替换将无法分辨哪个& 是哪个,所以我们需要使用一些上下文来弄清楚。

我们知道,当& 分隔两条路径时,下一条路径将以pathX= 形式的标签开始。我们可以使用模式&(?=(path\d+=)) 来匹配& 这样的字符。 (?=)zero-width positive lookahead assertion,这意味着其中的所有内容都必须遵循 &,但实际上不会匹配(替换)。 path\d+= 表示文字字符 path 后跟 one or more digits (\d+) 后跟文字字符 =

PS> $pattern = '&(?=(path\d+=))'
PS> 'path1=/path/me & you/file.json' -replace $pattern, ';'
path1=/path/me & you/file.json
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt&path3=/folder/R & B/ me & you/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt;path3=/folder/R & B/ me & you/file.txt
PS> '&path1=/path/me & you/file.json&path2=/path/you & me/file.txt' -replace $pattern, ';'
;path1=/path/me & you/file.json;path2=/path/you & me/file.txt
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt&' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt&
PS> 'path1=/path/me & you/file.json&&path2=/path/you & me/file.txt&&&&&path3=/folder/R & B/ me & you/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json&;path2=/path/you & me/file.txt&&&&;path3=/folder/R & B/ me & you/file.txt

请注意,前面的&(第四个测试输入)被替换,因为它后面跟着通常的path1=,但尾随&(第五个测试输入)和多个连续的&(最后一个测试输入)不是。如果这些情况应该被视为空路径而不是以& 结尾的文件名,则可以使用模式&+(?=(path\d+=)|$) 来完成。 &+ 现在将匹配 one or more &,添加 |$ 将在 end of the string 处出现 also match &

PS> $pattern = '&+(?=(path\d+=)|$)'
PS> 'path1=/path/me & you/file.json' -replace $pattern, ';'
path1=/path/me & you/file.json
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt&path3=/folder/R & B/ me & you/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt;path3=/folder/R & B/ me & you/file.txt
PS> '&path1=/path/me & you/file.json&path2=/path/you & me/file.txt' -replace $pattern, ';'
;path1=/path/me & you/file.json;path2=/path/you & me/file.txt
PS> 'path1=/path/me & you/file.json&path2=/path/you & me/file.txt&' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt;
PS> 'path1=/path/me & you/file.json&&path2=/path/you & me/file.txt&&&&&path3=/folder/R & B/ me & you/file.txt' -replace $pattern, ';'
path1=/path/me & you/file.json;path2=/path/you & me/file.txt;path3=/folder/R & B/ me & you/file.txt

【讨论】:

  • 不错的一个 (+1),IMO 显示将受益于一个包含所有变体的 here 字符串并在其上执行一个 -replace - 现在看起来有点拥挤。
  • @Levi 这最终解决了您的问题还是让您朝着正确的方向开始?
  • @Levi 很高兴听到它。请记住,如果您发现它有帮助/没有帮助,您可以在任何答案上 upvotedownvote 它,在 your own questions 上您可以 accept the answer 最能帮助您解决问题。
【解决方案2】:

你并不总是需要一些花哨的代码来一次性完成所有事情。

$test = $('path1=/path/me & you/file.json&path2=/path/you & me/file.txt' -split "&"); $test[0] + "&" + $test[1] + ";" + $test[2] + "&" + $test[3]

【讨论】:

  • 这假定输入 string 始终包含 path1path2,并且两条路径都恰好包含一个 &
  • @BACON 是的。
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 2015-01-19
  • 2013-04-14
相关资源
最近更新 更多