Author Posts

March 14, 2018 at 12:20 pm

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 🙂

March 14, 2018 at 12:44 pm

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

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

March 14, 2018 at 12:52 pm

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.

March 14, 2018 at 12:58 pm

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

   }

March 14, 2018 at 1:15 pm

Many thanks to both, will give those a try

didnt even consider the -le method, tunnel vision 🙂

March 14, 2018 at 9:13 pm

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

March 14, 2018 at 9:22 pm

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.

March 15, 2018 at 4:16 am

$a = import-csv num.csv
$a | where { $_.'Password Age (Days)' -in 0..90 } 

Password Age (Days)
-------------------
1                  
43                 
90                 
89                 
32                 

March 15, 2018 at 5:16 am

that's a neat one!

March 15, 2018 at 8:55 am

many ways to skin a cat here 🙂