Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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_Powershell_Escaping - Fatal编程技术网

Regex 如何创建使用正则表达式将文本中的字符串替换为选项卡的powershell脚本

Regex 如何创建使用正则表达式将文本中的字符串替换为选项卡的powershell脚本,regex,powershell,escaping,Regex,Powershell,Escaping,我有这样一个字符串: param ( [string] $text, [string] $inputPattern, [string] $replacePattern ) function Main() { $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern); [System.Console]::WriteLine(

我有这样一个字符串:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
“第1项”、“第2项”

我想使用powershell脚本替换它,使其看起来像这样:

项目1{tab character}项目2

我有这个:

$text = '"Item 1","Item 2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr2 = "$1\t$2"
$line = [System.Text.RegularExpressions.Regex]::Replace($text, $expr1, $expr2);
但它不起作用

另外,对于如何在Powershell中处理转义引号和特殊字符,是否有明确的参考?我觉得这确实很令人困惑

编辑:

我想这样做的原因是,我可以将其封装在一个参数化脚本中,并使用参数调用它。脚本(regex-rs.ps1)如下所示:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
不幸的是,当我这样调用脚本时:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
它输出:

Text1,Text2
换句话说,没有标签。我做错了什么

针对NICK的回答,进一步编辑如下:(我必须将此放在这里,因为StackOverflow中的注释格式与背景符号混淆)

在powershell调用中,我将单引号替换为双引号,如下所示:

powershell .\regex-rs.ps1 ""Text1","Text2"" "`"([^`"]+?)`",`"([^`"]+?)`"" "`$1`t`$2"
但我有一个错误:

Missing ] at end of type token.
还有其他想法吗

最终编辑:这是对解决问题的脚本的调用(必须以图像形式发布,因为它功能强大,即使在这里也无法设置StackOverflow的格式):


PowerShell中的转义字符是反勾号(`,与~)相同的键),要展开`t,需要将其括在引号中:

PS> $text -replace '","',"""`t"""
您还可以转义引号:

PS> $text -replace '","',"`"`t`""
在控制台中键入以下内容以获取更多帮助:

PS> Get-Help about_Escape_Characters

错误的是在“单引号字符串”中使用转义字符(`)。单引号字符串被视为文本。要使其正常工作,您需要使用“双引号”:

$text = '"Item 1","Item 2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr2 = "`$1`t`$2"
$line = [System.Text.RegularExpressions.Regex]::Replace($line, $expr1, $expr2);
-编辑-

我只在$expr2中将单引号替换为双引号。您遇到了问题,因为您在$text和$expr1中用双引号替换了单引号,但没有转义字符串中的引号字符

$text可以使用如下单引号:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
或者像这样的双引号(在字符串内部转义):

$expr1可以使用如下单引号:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
或者像这样的双引号:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
$expr2应仅使用如下双引号:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
-再次编辑-

事实上,我对你现在的问题不是很肯定。我知道我最初发布的这4行代码在powershell中工作。我从我的答案中复制了它们,并将它们粘贴到powershell控制台中,结果成功了。将字符串作为参数传递到我不知道的脚本上,我现在无法测试它。但请尝试我添加的内容,看看会发生什么

-编辑3-

这是可行的,但我不知道为什么必须第二次重复^1。如果我只有1^,它不会出现在字符串中,因此它不会匹配,但如果我将其加倍,它将非常有效。不知道为什么^1需要在一个位置加倍,但不在另一个位置

powershell .\regex.ps1 '\"Test1\",\"Test2\"' '\"([^"]+?)\",\"([^^\"]+?)\"' "`$1`t`$2"
在powershell中,如果要使用转义字符,必须使用双引号字符串并使用反勾(`)进行转义。在第一个和第二个字符串的命令行中,我必须使用单引号并使用反斜杠(\)对“字符”进行转义。出于某种原因,在第二个参数中,第二个^2需要重复两次,以便它实际出现在字符串中。我不知道为什么。对于第三个参数,当字符串被传递到powershell脚本中时,我必须再次使用backtick`使其正常工作


我责怪微软处理高度不一致的字符串。

如果在控制台中输入以下内容:

"This`tTest"
"Testing`tThis"
他们将获得:

This    Test
很明显,已经放置了一个选项卡

现在,如果将其输入控制台:

"This`tTest"
"Testing`tThis"
他们将得到:

Testing This
账单不见了吗?不,Powershell只将选项卡视为格式(就像尝试为表创建列一样),而不是绝对数量的空格。如果给Powershell一个字符串,该字符串将填充选项卡留下的全部空间,该怎么办

我们可以通过输入以下内容来测试这一点:

"Testings`tThis"
$inputPattern = '","'
$replacePattern = "`t"
我们最终得到以下输出:

Testings        This
恰好OP的$test string
“Item 1”,“Item 2”
遇到了上面的第二个测试用例,当
,“
被替换为
`t
时,它被“吃掉”,看起来就像一个空格,而不是一个制表符。事实上,Shay的答案是可行的,但由于选项卡的工作方式,它看起来根本不像(使用此字符串)

总之,我建议使用文字数量的空格而不是't',如下所示:

param
(
    [string] $text,
    [string] $inputPattern,
    [string] $replacePattern
)

function Main()
{
    $text2 = [System.Text.RegularExpressions.Regex]::Replace($text, $inputPattern, $replacePattern);
    [System.Console]::WriteLine($text2);
}

Main;
powershell .\regex-rs.ps1 '"Text1","Text2"' '`"([^`"]+?)`",`"([^`"]+?)`"' '`$1`t`$2'
$text = '"Text1","Text2"'
$expr1 = '"([^"]+?)","([^"]+?)"'
$expr1 = "`"([^`"]+?)`",`"([^`"]+?)`""
$expr2 = "`$1`t`$2"
$text -replace '","','     '
(这是最后两个
'
s之间的5个空格)

或者,如果此输出将由其他程序读取,则前面提到的解决方案:

$text -replace '","',"`t"
将工作,但你将不得不生活在Powershell显示它有趣

注:

如果您确实希望使用脚本,请执行以下操作:

"Testings`tThis"
$inputPattern = '","'
$replacePattern = "`t"

`t
如果用单引号括起来,它不会展开为选项卡
,但如果用双引号括起来,它会像变量一样展开。

我不知道这是否是您的问题,但不应该
$line=[System.RegularExpressions.Text]:Replace($Text,$expr1,$expr2);
be
$line=[System.Text.RegularExpressions.Regex]:Replace($Text,$expr1,$expr2);
?谢谢。修复了上面的问题,但这不是问题所在。请参阅上面我的第二次编辑。由于格式问题,无法将其添加到此处。请参阅我的编辑。我在注释中也遇到了格式设置问题。不幸的是,我确实需要它作为powershell脚本使用:(我认为这将是相当简单的,但我可能必须在这一点上提供悬赏…幸运的是,它在powershell脚本中确实起作用。这只是让命令行参数中的字符串正确传递的问题。看看上次的编辑。我刚刚测试了它,它确实起作用,我只是不知道为什么。顺便说一句@Nick,原因是没有你需要双插入符号是因为