# regex

**URL:** <https://forums.powershell.org/t/regex/12239>\
**Category:** PowerShell Help\
**Created:** [March 21, 2019, 1:18pm UTC](https://forums.powershell.org/t/regex/12239 "2019-03-21T13:18:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [March 21, 2019, 1:18pm UTC](https://forums.powershell.org/t/regex/12239/1 "2019-03-21T13:18:17Z")

</div>

hi all,

I’m new to regex and I’m trying a simple one to see how they work.

My simple regex is “(\w+@\w+\w+)” to be able to capture some email from a big text.

I’ve created a text file, put into it a big chunk of random text, and inserted an email at 2 spots in there.

When I compare the file’s content to my regex, -match shows me the 2 lines with the email but what I see is the whole line, not the email I was looking for.

If I do $matches, I see no match.

Can someone please explain what I am doing wrong?

$file = get-content file.txt

$regex = “(\w+@\w+\w+)”

$file -match $regex

Thank you!

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [March 21, 2019, 1:48pm UTC](https://forums.powershell.org/t/regex/12239/2 "2019-03-21T13:48:55Z")

</div>

I think you want to go through the file one line at a time. -match works differently with an array.

```
foreach ($line in $file) {
  $line -match $regex
  $matches
}

True

Name Value
---- -----
1 js@powershell
0 js@powershell
```

---

<div class="post-metadata">

**Author:** ![bo1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bo1/32/33_2.png) [@bo1](https://forums.powershell.org/u/bo1)\
**Post date:** [March 21, 2019, 2:17pm UTC](https://forums.powershell.org/t/regex/12239/3 "2019-03-21T14:17:41Z")

</div>

I would recommend using the [regex] accelerator. It allows the user to tap directly into .NET Regex, its faster and more efficient than using native PS to loop through data.

```
$text = Get-Content -Path C:\TEMP\PSdotorg_deleteMe.txt
$regex = "(\w+\@\w+\w+)"

[regex]::Matches($text,$regex)

Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 0
Length : 10
Value : this@email

Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 133
Length : 13
Value : another@email
```

---

<div class="post-metadata">

**Author:** ![bo1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bo1/32/33_2.png) [@bo1](https://forums.powershell.org/u/bo1)\
**Post date:** [March 21, 2019, 2:23pm UTC](https://forums.powershell.org/t/regex/12239/4 "2019-03-21T14:23:38Z")

</div>

Another recommendation is to use [RegEx101.com](https://regex101.com/) to double check your syntax. I personally find it easier to test with than running through PS to get it right, but to each their own.

```
$regex = "(\w+@\w+\.\w+)"
```

That regex will return the entire email address, including the .xyz

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [March 21, 2019, 2:51pm UTC](https://forums.powershell.org/t/regex/12239/5 "2019-03-21T14:51:58Z")

</div>

Even that pattern won’t work on emails with more than one ‘.’.

```
PS C:\users\js> 'js@www.powershell.org' -match "(\w+@\w+\.\w+)"; $matches
True

Name Value
---- -----
1 js@www.powershell
0 js@www.powershell
```

---

<div class="post-metadata">

**Author:** ![bo1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bo1/32/33_2.png) [@bo1](https://forums.powershell.org/u/bo1)\
**Post date:** [March 21, 2019, 6:37pm UTC](https://forums.powershell.org/t/regex/12239/6 "2019-03-21T18:37:35Z")

</div>

You are correct, unconventional formatting in the domain hadn’t occurred to me! The below expression accounts for that, provided of course there are no digits in use before the @. 🙂

```
$regex = '\w+@.+?(?=\s)'

[regex]::Matches($text,$regex)

Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 0
Length : 17
Value : email@address.com

Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 150
Length : 27
Value : another@email.different.com

Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 222
Length : 47
Value : whatTheHeck@www.why.onearth.areyoudoingthis.com
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 21, 2019, 7:49pm UTC](https://forums.powershell.org/t/regex/12239/7 "2019-03-21T19:49:22Z")

</div>

An email address might be the wrong choice for a regex beginner … even when most of the people think that an email address is easy to recognize.

This site illustrates a little bit of what I mean: [http://emailregex.com/](http://emailregex.com/).

---

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [March 22, 2019, 6:00am UTC](https://forums.powershell.org/t/regex/12239/8 "2019-03-22T06:00:18Z")

</div>

ok I will experiment with what you guys gave me and I believe I’ll be good.

Thank you!

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 22, 2019, 5:26pm UTC](https://forums.powershell.org/t/regex/12239/9 "2019-03-22T17:26:27Z")

</div>

Messing with text with email strings.

```
$UrlList = @'
this is the URL https://stackoverflow.com/&20%
http://stackoverflow.com
http://www.SomeSite.com this is oure main site
http://www.SomeSite.com
ftp://www.somesite.com
ftp://somesite.com
ftp\SomeSite.com
If you want the file go there: file://SomeSite.com
'@ 

[RegEx]::Matches($UrlList, '(ftp:|ftp|http:|https:|file:)(//.([^\s]+)|\\.([^\s]+))').value

https://stackoverflow.com/&20%
http://stackoverflow.com
http://www.SomeSite.com
http://www.SomeSite.com
ftp://www.somesite.com
ftp://somesite.com
ftp\SomeSite.com
file://SomeSite.com
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/regex/12239/10 "2024-05-16T20:33:06Z")

</div>


