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
Regex 使用replace清理选择字符串输入_Regex_Parsing_Powershell_Replace - Fatal编程技术网

Regex 使用replace清理选择字符串输入

Regex 使用replace清理选择字符串输入,regex,parsing,powershell,replace,Regex,Parsing,Powershell,Replace,我正在解析日志并获取输出,但我想清理输出。目前,我在输出中获得了日志文件的完整路径,该路径很长,不需要。下面的代码是我试图使用的,但是我得到一个错误,我的replace不是有效的正则表达式 select-string $_ -pattern "gam.exe : Error 409: Entity already exists" -context 1,0 | ForEach-Object{$_ -replace "D:\test1\user\mail\mail-Load\logs","" }

我正在解析日志并获取输出,但我想清理输出。目前,我在输出中获得了日志文件的完整路径,该路径很长,不需要。下面的代码是我试图使用的,但是我得到一个错误,我的replace不是有效的正则表达式

select-string $_ -pattern "gam.exe : Error 409: Entity already exists" -context 1,0 | ForEach-Object{$_ -replace "D:\test1\user\mail\mail-Load\logs","" }

我不确定我需要做什么才能得到正确的替换输入,我需要转义字符吗

反斜杠是正则表达式转义字符。要将其用作正则表达式中文字匹配的一部分,必须使用另一个反斜杠对其进行转义。如果您不确定哪些字符需要转义,[regex]有一个转义方法,您可以使用它来转义所有保留字符:

[regex]::escape("D:\test1\user\mail\mail-Load\logs")
D:\\test1\\user\\mail\\mail-Load\\logs
所以

是用于匹配文本字符串的正则表达式

'D:\test1\user\mail\mail-Load\logs'

你试过用单引号而不是双引号吗?我试过了,但似乎没有改变错误。它不像“D:\test1\user\mail\mail Load\logs”或“D:\test1\user\mail\mail Load\logs”那样有效,我只需要跳出\就可以了,它现在可以正常过滤了。非常感谢。
'D:\test1\user\mail\mail-Load\logs'