g_l
g_l

Reputation: 661

Match anything except e-mail address in line

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

The following regex matches the e-mail addresses

([^\s]+)\b[@]+\b([^\s]+)

How can I make it the opposite?

Upvotes: 1

Views: 42

Answers (2)

tukan
tukan

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

Esperento57
Esperento57

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

Related Questions