none
Need help in extracting numbers from different strings RRS feed

  • Question

  • We have a values of different object in txt file.

    Text files values look like

    vDS_lgcy_V342_SDF

    VDS_strch_V234_SDF

    SW1_VLAN342

    I need only to get 3 digit values out of it. Can anyone helps me in this. I found no logic.

    Saturday, February 25, 2017 4:16 PM

Answers

  • Thanks ......

    I am able to make a code to solve my problem using Replace.

    $array=@()
    foreach ( $i in $a ) { $b = $i -replace '\D+(\d+)\D+','$1'
                           if  ( $b.length -eq 3 ) 
                                { $array += $b }
                           else  { $b = $b%1000
                                   $array += $b }
                        }
    $array
    

    • Marked as answer by asharma5 Sunday, February 26, 2017 5:27 AM
    Sunday, February 26, 2017 5:27 AM

All replies

  • Hint:


    PS C:\> help about_Regular_Expressions


    -- Bill Stewart [Bill_Stewart]

    Saturday, February 25, 2017 5:30 PM
    Moderator
  • Thanks ......

    I am able to make a code to solve my problem using Replace.

    $array=@()
    foreach ( $i in $a ) { $b = $i -replace '\D+(\d+)\D+','$1'
                           if  ( $b.length -eq 3 ) 
                                { $array += $b }
                           else  { $b = $b%1000
                                   $array += $b }
                        }
    $array
    

    • Marked as answer by asharma5 Sunday, February 26, 2017 5:27 AM
    Sunday, February 26, 2017 5:27 AM
  • # Example using Select-String (sls)
    $array = $a | foreach {
        sls -input $_ -pattern '\d{3}'|
            select -expand matches | 
            select -expand value
    }
    $array
    
    # Example using -match operator
    $array = $a | foreach {
        if($_ -match '\d{3}') {
            $matches[0]
        }
    }
    $array


    Sunday, February 26, 2017 6:09 AM