Problems pulling numbers in text string...

Martin Clark 21 Reputation points
2020-10-20T16:19:10.063+00:00

Hi All,

I have a large array of string values, each of which are reasonably the same but I need to retrieve three digits which make up the VLAN id from a network adapter object

An example string is as follows:

Domain|TestDevGroup|IP-10.20.30.0-VL222-Ref

I have managed to create the following Regular expression: VL(.+)[0-9]{2,3}

This gives me the result 'VL222' which is near to what I want but does anyone know how I can select only the digits without the leading VL?

Also, I cant find out how I apply this query against an array string in PowerShell

for example if the network names were a single column in a .csv file:

$nics = import-csv nics.csv

foreach($nic in $Nics){

$Vlan = $nic | select-string -pattern 'VL(.+)[0-9]{2,3}'

}

Does not seem to work

Hopefully one of you can shed some light on a newb like me?

Many thanks guys,

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 36,071 Reputation points Microsoft Vendor
    2020-10-21T06:56:53.817+00:00

    Hi,

    If you want to match the digits following VL, you could use zero-width positive lookbehind assertions and the regex could be (?<=VL)\d{2,3}

    $nics = Import-Csv C:\nics.csv  
    $Vlan = foreach($nic in $nics){  
    if($nic.YourColumnName -match "(?<=VL)\d{2,3}"){$matches.Values}  
    }  
    

    https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#zero-width-positive-lookbehind-assertions

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Andreas Baumgarten 107.5K Reputation points MVP
    2020-10-20T17:09:03.283+00:00

    Maybe something like this?

    $nic = "Domain|TestDevGroup|IP-10.20.30.0-VL222-Ref"
    $vlan = ($nic -split("-") -replace "VL","")[2]
    $vlan
    

    $vlan contains "222"

    And this is the foreach piece (not tested):

    $nics = import-csv nics.csv
    foreach($nic in $nics){
           $vlan = ($nic -split("-") -replace "VL","")[2]
           $vlan
           }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Rich Matheisen 46,476 Reputation points
    2020-10-20T18:53:31.047+00:00

    If you're going to ask the same question in multiple places, please have the curtesy to leave a link to the other question!
    problems-pulling-numbers-in-text-string
    I answered your question in the "Scripting Guys" forum.

    Your regex is incorrect. It matches the 1st character after "VL" and "memorizes" it (because it in a group), and them matches the next two digits -- but it will also match the next three -- so it'll also match "VL2222". That's not what you want.

    $s = "Domain|TestDevGroup|IP-10.20.30.0-VL222-Re"
    $s -match "VL(.+)[0-9]{2,3}"
    True
    
    $matches
    
    Name                           Value
    ----                           -----
    1                              2
    0                              VL222
    

    The regex that does what you ask is "-VL(\d\d\d?)-", which uses the hyphens as a sort of anchor. That's not entirely necessary, but it adds a bit of certainty to the outcome.

    $s = "Domain|TestDevGroup|IP-10.20.30.0-VL222-Re"
    $VLAN = $null
    if ($s -match "-VL(\d\d\?)-"){
        $VLAN = $matches[1]
    }
    

    To extract the string you want from a CSV, try this:

    import-csv nics.csv |
        Foreach-Object{
            $Vlan = $null
            if ($_.COLUMNNAME -match '-VL(\d\d\d?)-'){
                $Vlan = $matches[1]
            }
            etc.
            etc.
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.