-Match on a substring length?

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 SERVER-1-TEMP
SERVER-9 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.

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 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

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}'

'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:

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

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?

$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"
}

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. :+1:t3:

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.

$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 …

$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!! :point_up:t3:

That works great!

Thank you.