Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/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
String 非平凡的分解字符串到集合_String_Powershell_Collections_Explode - Fatal编程技术网

String 非平凡的分解字符串到集合

String 非平凡的分解字符串到集合,string,powershell,collections,explode,String,Powershell,Collections,Explode,我需要一个PS函数,该函数将获取输入字符串并生成输出集合,如下所示: 输入: $someString = "abcd{efg|hijk|lmn|o}pqrs" 期望输出: $someCollection = @("abcdefgpqrs","abcdhijkpqrs","abcdlmnpqrs","abcdopqrs") 注意:输入字符串中最多有1个{…|…|…}表达式;管道的数量是动态的,可以是1到20之间的任何数量 当我驱动输入数据时,要分解的字符串的格式不必完全遵循上面的示例;它可以是

我需要一个PS函数,该函数将获取输入字符串并生成输出集合,如下所示:

输入:

$someString = "abcd{efg|hijk|lmn|o}pqrs"
期望输出:

$someCollection = @("abcdefgpqrs","abcdhijkpqrs","abcdlmnpqrs","abcdopqrs")
注意:输入字符串中最多有1个{…|…|…}表达式;管道的数量是动态的,可以是1到20之间的任何数量

当我驱动输入数据时,要分解的字符串的格式不必完全遵循上面的示例;它可以是其他任何东西;我追求的是简单而不是复杂

我的问题是,有没有基于RegExp的解决方案可以直接使用,或者我应该从头开始编写函数,分析输入字符串,检测所有的{s、| s和}等等


平台:Windows 7/Windows Server 2012,PowerShell 5.x

使用PowerShell 5和正则表达式,您可以非常轻松地做到这一点:

# define a regex pattern with named groups for all three parts of your string
$pattern = '^(?<pre>[^\{]*)\{(?<exp>.*)\}(?<post>[^\}]*)$'

if($someString -match $pattern){
    # grab the first and last parts
    $prefix = $Matches['pre']
    $postfix = $Matches['post']

    # explode the middle part
    foreach($part in $Matches['exp'] -split '\|'){
        # create a new string for each of the exploded middle parts
        "$prefix$part$postfix"
    }
}
#为字符串的所有三个部分定义一个带有命名组的正则表达式模式
$pattern='^(?[^\{]*)\{(?..*\}(?[^\}]*)$'
if($someString-匹配$pattern){
#抓住第一部分和最后一部分
$prefix=$Matches['pre']
$postfix=$Matches['post']
#炸中间
foreach($Matches['exp']-split'\\中的部分){
#为每个分解的中间部分创建一个新字符串
“$prefix$part$postfix”
}
}

使用PowerShell 5和正则表达式,您可以非常轻松地做到这一点:

# define a regex pattern with named groups for all three parts of your string
$pattern = '^(?<pre>[^\{]*)\{(?<exp>.*)\}(?<post>[^\}]*)$'

if($someString -match $pattern){
    # grab the first and last parts
    $prefix = $Matches['pre']
    $postfix = $Matches['post']

    # explode the middle part
    foreach($part in $Matches['exp'] -split '\|'){
        # create a new string for each of the exploded middle parts
        "$prefix$part$postfix"
    }
}
#为字符串的所有三个部分定义一个带有命名组的正则表达式模式
$pattern='^(?[^\{]*)\{(?..*\}(?[^\}]*)$'
if($someString-匹配$pattern){
#抓住第一部分和最后一部分
$prefix=$Matches['pre']
$postfix=$Matches['post']
#炸中间
foreach($Matches['exp']-split'\\中的部分){
#为每个分解的中间部分创建一个新字符串
“$prefix$part$postfix”
}
}

非常优雅,有点类似于我当时提出的:
函数分解字符串([String]$s){$retVal=New Object'System.Collections.Generic.List[String]'if($s-like'*{*.*.*.*}和$s-notlike'*{*.*.*{*.*.*.*}'){$chunks=$s-Split{$.-eq'{-或$.-eq}foreach($chunks[1].Split(“|”)中的$c{$retVal.Add($chunks[0]+$c+$chunks[2])}其他{$retVal.Add($s)}返回@(,$retVal)}
非常优雅,有点类似于我当时提出的:
函数分解字符串([String]$s){$retVal=New Object'System.Collections.Generic.List[string]'if($s-like'*{*.*}.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.{$chunks=$s-Split{$$.-eq'''.}foreach($chunks[1]中的c.Split('.'.'''.'''''''.''){$retVal.Add chunks[0]+$c+$chunks[2]}.}.}其他{$retVal.Add($s)}return@(,$retVal)}