我遇到了一个无法解决的正则表达式问题:
我的字符串是这样的:
Y
[sudo] Password for user: Other Text here
even more text here: with more text
我的目标是选择从第一行开始到第一个“:”(包括“:”)的所有文本,并将其替换为无。问题在于前两行可能存在,也可能不存在,仍需要选择文本,包括第一个“:”。我的最终结果应该如下:
Other Text here
even more text here: with more text
这是我得到的,但现在卡住了:
$Test[0..3] -replace ('[a-zA-Z]+\w\:*\s')
但这还剩下了0行和1行,也没有去掉[sudo] :(
希望正则表达式专家能给我一些见解 :)
编辑 有关实际字符串的更多信息(它非常长,包含一个Unix日志文件):
C:\> $MainArray
y
[sudo] password for User: 1485673353 1 33412 2 appxxx 1801152 1801047 0 appxxx bptm image copy is not ready, retry attempt: 6 of 500 o
bject is busy, cannot be closed
1485673354 1 33412 2 appxxx 1801153 1801047 0 appxxx bptm image copy is not ready, retry attempt: 6 of 500 object is busy, cannot be closed etc. etc.
尝试第一个答案: C:> $MainArray -replace ('^[^:]+?:\s*')
y
1485676476 1 4 4 appxxx 1801540 1801213 0 appxxx bptm successfully wrote backup id appxxx_1485671817, copy 1, fragment 17, 51200000
它似乎没有删除前两行(y和RETURN)
$Test[0]
返回的是字符串的第一个字符,而不是第一行。通过使用上述正则表达式,您无需将字符串分割为前三行,因为它只从字符串开头进行搜索。如果您仍然想将其分割为行并仅搜索前n
行,则可以使用($Test -split "\r?\n")[0]
。 - ssc-hrep3