# Regex question

**URL:** https://forums.powershell.org/t/regex-question/7882
**Category:** PowerShell Help
**Created:** [January 19, 2017, 12:46pm UTC](https://forums.powershell.org/t/regex-question/7882 "2017-01-19T12:46:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![simee](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@simee](https://forums.powershell.org/u/simee)
#### Post date: [January 19, 2017, 12:46pm UTC](https://forums.powershell.org/t/regex-question/7882/1 "2017-01-19T12:46:20Z")

</div>

I am working with the following match statement:

```
$folder -match "[A-Z][A-Z][A-Z][A-Z][A-Z0-9]_*"
```

However, it does not work the way I would it expect to work. I would expect

```
$folder="ABCDE-SomeOtherText"
```

to be false, and

```
$folder="ABCDE_SomeOtherText"
```

to be true.

Unfortunately, both arguments are true. How can I test if the 6th position is an underscore (\_) and not anything else ?

Thanks!

---

<div class="post-metadata">

### Author: ![chris-bakker](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chris-bakker/32/6356_2.png) [@chris-bakker](https://forums.powershell.org/u/chris-bakker)
#### Post date: [January 19, 2017, 1:36pm UTC](https://forums.powershell.org/t/regex-question/7882/2 "2017-01-19T13:36:37Z")

</div>

This should do it:

```
^[A-Za-z]{5}_.+
```

^ marks the begin  
[A-Za-z] = All A-Z ad a-z  
{5} = 5 times [A-Za-z]  
[\_] = an underscore

---

<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: [January 19, 2017, 1:39pm UTC](https://forums.powershell.org/t/regex-question/7882/3 "2017-01-19T13:39:17Z")

</div>

```
$folder1="ABCDE-SomeOtherText"
$folder2="ABCDE_SomeOtherText"
$Regex = '[A-Z]{5}_.+'
$folder1 -match $Regex
$folder2 -match $Regex
```

---

<div class="post-metadata">

### Author: ![simee](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@simee](https://forums.powershell.org/u/simee)
#### Post date: [January 19, 2017, 1:45pm UTC](https://forums.powershell.org/t/regex-question/7882/4 "2017-01-19T13:45:02Z")

</div>

Thanks, works like a charm!

---

<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:39pm UTC](https://forums.powershell.org/t/regex-question/7882/5 "2024-05-16T20:39:24Z")

</div>


