Need a tad of help with a regex

I've been working on this for three days now. Thought it would be easy (is in cmd... lol)

I have a said number of files that I need to script some moves to certain folders. I've done it many times; however, this time I just can't get the right regex

Anyway, I need to match  two static characters at the beginning of a string and a few characters at the end of the string.  Not caring about what is in the middle of the two points.  Seems easy; but, ...   Something like   ab*-05.pdf   works perfectly in cmd

I know it is going to come fast to someone

May 22nd, 2015 2:46pm

$s -match '^[a|b].*xyz$'

PS C:\scripts> $s='asddfdsdsfdsxyz'
PS C:\scripts> $s -match '^[a|b].*xyz$'
True
PS C:\scripts> $s='bsddfdsdsfdsxyz'
PS C:\scripts> $s -match '^[a|b].*xyz$'
True
PS C:\scripts> $s='psddfdsdsfdsxyz'
PS C:\scripts> $s -match '^[a|b].*xyz$'
False
PS C:\scripts>

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 3:11pm

The Get-ChildItem (alias: dir, ls, gci) cmdlet's -Filter parameter accepts wildcards. Take a look at the image and I'll quickly explain everything that happens at each prompt (PS C:\>).

1. Return all the files in the folder C:\files01.
2. Return the files in folder C:\files01 that begin with 'ab' and end with '05.pdf.'
3. Return all the files in the folder C:\files02 (there are none).
4. Rerun the command that returns only the files that begin with 'ab' and end with '05.pdf,' piping the results to the Move-Item cmdlet. This will move them from C:\files01 to C:\files02.
5. Return all the files in the folder C:\files02 (now there are three files).
6. Return all the files in the folder C:\files01 (it's missing three files now).

Hope this helps!

May 22nd, 2015 3:16pm

Think I had not stated my issue very well

I need   ab  and  -04.pdf to be a match

So files named like

ab123456-04.pdf
abuax853-04.pdf
ab4acm94lshjopiu03-04.pdf

Would all be a match

Actually being used in a switch statement

Now if I test like:

[PS] C:\Scripts> $a = 'ab123456-04.pdf'
[PS] C:\Scripts> $a -match '^ab\d+-04.pdf$'
True

It works; however, that is only going to work with digits in the middle where I need it for alphanumeric



Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 3:25pm

Think I had not stated my issue very well

I need   ab  and  -04.pdf to be a match

So files named like

ab123456-04.pdf
abuax853-04.pdf
ab4acm94lshjopiu03-04.pdf

Would all be a match

Actually being used in a switch statement

Now if I test like:

[PS] C:\Scripts> $a = 'ab123456-04.pdf'
[PS] C:\Scripts> $a -match '^ab\d+-04.pdf$'
True

It works; however, that is only going to work with digits in the middle where I need it for alphanumeric



May 22nd, 2015 3:27pm

This should work:

$a -match '^ab\w+-04.pdf$'

  • Proposed as answer by jrv 11 hours 28 minutes ago
  • Marked as answer by wrecked.admin 11 hours 24 minutes ago
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 3:33pm

This should work:

$a -match '^ab\w+-04.pdf$'

  • Proposed as answer by jrv 11 hours 22 minutes ago
  • Marked as answer by wrecked.admin 11 hours 17 minutes ago
May 22nd, 2015 3:35pm

Yes, I was hoping to stay away from having to do a bunch of 
if (gci -path -filter){ move-item } statements though

Was hoping to do just one get-childitem (all files to be moved are in one folder so...), define the destination folder within the switch/regex statements and do and...

But hey, if I can't get the regex within the switch, I may just go this way

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 3:40pm

Yes, I was hoping to stay away from having to do a bunch of 
if (gci -path -filter){ move-item } statements though

Was hoping to do just one get-childitem (all files to be moved are in one folder so...), define the destination folder within the switch/regex statements and do and...

But hey, if I can't get the regex within the switch, I may just go this way

Your request is quite vague and convoluted.  That is why you keep getting different answers that you are not happy with.

May 22nd, 2015 3:44pm

Exactly what I was looking for.  Knew there was something I was missing...

Thanks All and have a Great Memorial Day weekend (US people). 

And for those that have served to help keep your country (wherever) safe - Thank You too

Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 3:50pm

Think I had not stated my issue very well

I need   ab  and  -04.pdf to be a match

So files named like

ab123456-04.pdf
abuax853-04.pdf
ab4acm94lshjopiu03-04.pdf

Would all be a match

Actually being used in a switch statement

Now if I test like:

[PS] C:\Scripts> $a = 'ab123456-04.pdf'
[PS] C:\Scripts> $a -match '^ab\d+-04.pdf$'
True

It works; however, that is only going to work with digits in the middle where I need it for alphanumeric



Not sure why you need that regex:

gci -filter 'ab*-04.pdf'

May 22nd, 2015 3:51pm

Yes, I was hoping to stay away from having to do a bunch of 
if (gci -path -filter){ move-item } statements though

Was hoping to do just one get-childitem (all files to be moved are in one folder so...), define the destination folder within the switch/regex statements and do and...

But hey, if I can't get the regex within the switch, I may just go this way

You only have to do one Get-ChildItem | Move-Item. The rest of what I included was to allow you to see the transitions of the files in the beginning and end, and in the first two steps, the difference between filtering and not filtering.
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 4:26pm

Think I had not stated my issue very well

I need   ab  and  -04.pdf to be a match

So files named like

ab123456-04.pdf
abuax853-04.pdf
ab4acm94lshjopiu03-04.pdf

Would all be a match

Actually being used in a switch statement

Now if I test like:

[PS] C:\Scripts> $a = 'ab123456-04.pdf'
[PS] C:\Scripts> $a -match '^ab\d+-04.pdf$'
True

It works; however, that is only going to work with digits in the middle where I need it for alphanumeric



  • Edited by wrecked.admin Friday, May 22, 2015 7:25 PM readability
May 22nd, 2015 7:24pm

This should work:

$a -match '^ab\w+-04.pdf$'

  • Proposed as answer by jrv Friday, May 22, 2015 7:40 PM
  • Marked as answer by wrecked.admin Friday, May 22, 2015 7:45 PM
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 7:31pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics