# \-Match on a substring length?

**URL:** <https://forums.powershell.org/t/match-on-a-substring-length/22820>\
**Category:** PowerShell Help\
**Created:** [August 29, 2023, 3:53pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820 "2023-08-29T15:53:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![palmero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/palmero/32/1097_2.png) [@palmero](https://forums.powershell.org/u/palmero)\
**Post date:** [August 29, 2023, 3:53pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/1 "2023-08-29T15:53:32Z")

</div>

Hi everyone.

Is there a way to match based on string length?

I am trying to match a .csv that contains entries with a single server name to an object with multiple servers. Some object server names have the FQDN, others do not.

Input spreadsheet:  
SERVER-1  
SERVER-1-TEMP  
SERVER-2  
SERVER-3  
… etc.

Server objects:  
SERVER-0 [SERVER-DMZ.mydomain.com](http://SERVER-DMZ.mydomain.com) SERVER-1-TEMP  
SERVER-9 [SERVER-10.mydomain.com](http://SERVER-10.mydomain.com) SERVER-10-TEMP  
… etc.

ForEach($Server in $Servers) {  
If($Object.Name -match $Server) {  
…  
…  
}  
}

In the above example, both SERVER-1 and SERVER-1-TEMP get a hit on the first server object and also on the second (SERVER-10-TEMP). I would like to get a hit only on SERVER-1-TEMP (13 characters) and not SERVER-1 (8 characters).

Any suggestions greatly appreciated.

---

<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:** [August 29, 2023, 4:09pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/2 "2023-08-29T16:09:21Z")

</div>

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

If you’re looking for a specific length of a string you could use the automatic property `'.Length'` or you use a regex pattern with a quantifier `'[a-z0-9-]{13}'`

```auto
'SERVER-1-TEMP'.length
'SERVER-1'.Length
'SERVER-1-TEMP' -match '[a-z0-9-]{13}'
'SERVER-1' -match '[a-z0-9-]{13}'

```

The output looks like this:

```auto
PS C:\_Sample> 'SERVER-1-TEMP'.length
13
PS C:\_Sample> 'SERVER-1'.Length
8
PS C:\_Sample> 'SERVER-1-TEMP' -match '[a-z0-9-]{13}'
True
PS C:\_Sample> 'SERVER-1' -match '[a-z0-9-]{13}'
False

```

---

<div class="post-metadata">

**Author:** ![palmero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/palmero/32/1097_2.png) [@palmero](https://forums.powershell.org/u/palmero)\
**Post date:** [August 29, 2023, 6:55pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/3 "2023-08-29T18:55:27Z")

</div>

Thank you for your reply.

I understand the .length and regex statements. What’s eluding me is incorporating them in an if statement using -match between two values.

Although the following doesn’t return an error, it doesn’t work. In this case, it should not match. It also appears that the variable $length does not work inside a regex statement.

Am I coding it correctly?

```auto
$source = "SERVER-01"
$target = "Long string with SERVER-01-DEV to be matched"

$length = $source.length
If($source -match $target.'[a-z0-9-]{$length}') {
    Write-Host "Matched"
}

```

---

<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:** [August 29, 2023, 7:15pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/4 "2023-08-29T19:15:44Z")

</div>

Sorry I guess I misunderstood your requirement.

First I thought you could use word boundaries. But unfortunately hyphens are considered word boundaries. But assumed you picked an example similar to your real world data you could use white space instead of word boundaries. 👍🏼

If you’re looking for a single word in a longer text you could _decorate_ the word you’re looking for with white space regex on both sides and use this as regex pattern.

```auto
$source = "SERVER-01"
$target = "Long string with SERVER-01-DEV to be matched"

$RegexSearchPattern = '\s{0}\s' -f $source
$target -match $RegexSearchPattern
$matches

```

As you can see it does not match.  
While this …

```auto
$target = "Long string with SERVER-01 to be matched"
$target -match $RegexSearchPattern
$matches

```

… does match.

Please be aware that you should remove or empty the automatic variable `$Matches` after each loop iteration. Otherwise your previous result could affect the next one!! ☝🏼

---

<div class="post-metadata">

**Author:** ![palmero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/palmero/32/1097_2.png) [@palmero](https://forums.powershell.org/u/palmero)\
**Post date:** [August 29, 2023, 9:36pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/5 "2023-08-29T21:36:05Z")

</div>

> [@Olaf](#):
>
> ```auto
> $source = "SERVER-01"
> $target = "Long string with SERVER-01-DEV to be matched"
> 
> $RegexSearchPattern = '\s{0}\s' -f $source
> $target -match $RegexSearchPattern
> $matches
> 
> ```

That works great!

Thank you.

---

<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:14pm UTC](https://forums.powershell.org/t/match-on-a-substring-length/22820/6 "2024-05-16T20:14:39Z")

</div>


