PowerShell Regex用于重复字符

时间:2014-08-19 07:28:35

标签: regex powershell

我有一个CSV文件,有时包含我想要从结果中过滤掉的空行。当我知道我的输入时,我知道要预期多少commas并将其过滤掉:

"Test 1, Text A,", "Test 2, Text B,", ",," | Where {$_ -ne ",," }

正则表达式如何过滤空行而不管有多少commas?因为我从来不知道会有多少列。

在以下示例中,我希望能够检索与上一个示例中相同的结果:

"Test 1, Text A,", "Test 2, Text B,", ",,", ",", ",,," | Where {$_ -ne ",," }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

"Test 1, Text A,", "Test 2, Text B,", ",,", ",", ",,," 
         | Where {!([regex]"^[,]+$").match($_).Success}

搜索开头之间有多个逗号的行 (char ^)和字符串的结尾(char $

您还可以将此表达式简化为以下值:

"Test 1, Text A,", "Test 2, Text B,", ",,", ",", ",,," | Where {$_ -notmatch "^[,]+$"}