Reputation: 107
I am struggling with a regular expression that I can't figure out:
My string is like this
Y
[sudo] Password for user: Other Text here
even more text here: with more text
My goal is to select all the text from the first line up to and including the first ': ' and to replace it with nothing.
Problem is that the first two lines might or might not be present, and it would still have to select the text up to and including the first occurrence of ': '
My end result should look like:
Other Text here
even more text here: with more text
This is what I got, but now I am stuck:
$Test[0..3] -replace ('[a-zA-Z]+\w\:*\s')
But that leaves line 0 and line 1 and doesn't get rid of [sudo] either :(
Hope a regex wiz can give me some insight :)
EDIT Some more info on the actual string (which is very long and contains a unix log file):
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.
Trying the first answer: 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
It somehow doesn't delete the first two lines (the y and the RETURN)
Upvotes: 4
Views: 20482
Reputation: 16079
You can use this regular expression:
^[^:]+?(?=:)
^
matches the beginning of the String[^:]+?
matches 1 or more characters except :
with as few characters as possible(?=:)
a positive lookahead: a colon needs to be directly after the previous matchIf you want to match the colon and the next space too, you can also use this regular expression:
^[^:]+?:\s*
:
instead of (?=:)
includes the colon in the match\s*
matches as much whitespace characters as possible (0 or more whitespace characters)$test="[sudo] password for user: other text here. even more text: sdfvsdv"
$test -replace "^[^:]+?:\s*"
other text here. even more text: sdfvsdv
Upvotes: 5