Powershell / Regex

Welcome Forums General PowerShell Q&A Powershell / Regex

This topic contains 9 replies, has 6 voices, and was last updated by

 
Participant
8 months ago.

  • Author
    Posts
  • #95894

    Participant
    Points: 20
    Rank: Member

    Hi Guys

    building a script to interegate a csv,

    essentially the csv looks like

    
    Password Age (Days)
    1
    43
    568
    90
    89
    32
    356
    

    i need to capture ranges 0-90, i tried the below but get 0-90 **MISSING OUT THE SINGLE NUMBER 9**

    $_.'Password Age (Days)' -match "^(90|[0-8][0-9])$"

    i can see its this part "[0-8]" but dont know how to correct without then getting 91,92,93,94 etc...

    my regex sucks and i know it 🙂

  • #95901
    Ron

    Participant
    Points: 0
    Rank: Member

    https://regex101.com/r/aYpKl8/1

    I like this site for developing and testing regex. It will even show interactive help for each element.

  • #95904
    Ron

    Participant
    Points: 0
    Rank: Member

    https://regex101.com/r/aYpKl8/3

    Updated, realized it would get all the single digits except 9. This is probably not the only solution either, I am just a regex hack too, but with slightly more experience.

  • #95907

    Participant
    Points: 1
    Rank: Member

    hi,

    I'm bad with regex so I'll try to avoid them. Why don't you use less or equal operator and if statement?

    
    import-csv C:\temp\psorg.csv | foreach {
    
       if ([int]$_.'Password Age (Days)' -le 90) {$_.'Password Age (Days)'}
    
       }
    
  • #95912

    Participant
    Points: 20
    Rank: Member

    Many thanks to both, will give those a try

    didnt even consider the -le method, tunnel vision 🙂

  • #95979

    Participant
    Points: 207
    Helping Hand
    Rank: Participant

    But if you really wanted RegEx, you could do this...

        
        $PWAge = @'
        1
        43
        568
        90
        89
        32
        356
        '@
    
        [regex]::Matches($PWAge,'(?< !\d)\d{1,2}(?!\d)').Value
    
        1
        43
        90
        89
        32
    
  • #95981

    Participant
    Points: 36
    Rank: Member

    This regex tool is pretty good may help you.. https://www.workshell.co.uk/products/netregex

    I used it to help me with this regex:
    (.*\=.*|\n.*\=)(.*|\n.*)\(DESCRIPTION*.\=

    I was parsing Oracle Tnsnames files.

  • #95988
    js

    Participant
    Points: 207
    Helping Hand
    Rank: Participant
    $a = import-csv num.csv
    $a | where { $_.'Password Age (Days)' -in 0..90 } 
    
    Password Age (Days)
    -------------------
    1                  
    43                 
    90                 
    89                 
    32                 
    
  • #95996

    Participant
    Points: 20
    Rank: Member

    many ways to skin a cat here 🙂

The topic ‘Powershell / Regex’ is closed to new replies.