Reputation: 661
In the following lines
Doe, John [email protected]
Bar, Foo [email protected]
Example, Michael (Contract Worker) [email protected]
How can I make a regex to return only
Doe, John
for line 1, Bar, Foo
for line 2, and Example, Michael (Contract Worker)
for line 3 while discarding only the
e-mail addresses?The following regex matches the e-mail addresses
([^\s]+)\b[@]+\b([^\s]+)
How can I make it the opposite?
Upvotes: 1
Views: 42
Reputation: 17347
It seems easier to capture everything before email (considering that you have the spaces there) like this:
^.*(?<=\s{4})
With tabs you whould have to do it like this:
^.*(?<=\t)
Edit - due to comment (noted by g_l):
In this case ^.*(?<=\s{1})
is enough do to the regex greedy nature and email not having any spaces within.
Upvotes: 4
Reputation: 17472
try something like this, split by space and remove last element:
Get-Content "C:\temp\test.txt" |%{
$Result=$_.Trim() -split ' '
$Result[0..($Result.Length -2)] -join ' '
}
Upvotes: 2