Powershell / Regex

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

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

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

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.

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

   }

Many thanks to both, will give those a try

didnt even consider the -le method, tunnel vision :slight_smile:

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

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.

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

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

that’s a neat one!

many ways to skin a cat here :slight_smile: