0

For an Exchange Online rule I'm trying to create a regular expression in PowerShell that matches (-match) any case that does NOT end with '.pdf'. Sadly I'm not that experienced yet with regular expressions. I'm using the following regex testing site to test.

http://regexstorm.net/tester

It seems to match the output you would get from PowerShell regex.

I have already tried the following: .?[^\.pdf]$.

This is what I get for output:

  • "test.txt" -match '.?[^\.pdf]$' → True (this is what I want)
  • "test.docx -match '.?[^\.pdf]$' → True (this is what I want)
  • "test.pdf.txt -match '.?[^\.pdf]$'→ True (this is what I want)
  • "test.pdf" -match '.?[^\.pdf]$' → False (this is what I want)

It starts to fail with the following examples:

  • "test.pdd" -match '.?[^\.pdf]$' → False (this is NOT what I want)
  • "test.pdx" -match '.?[^\.pdf]$' → False (this is NOT what I want)

I have also tried many things along the lines of: '.?(\.[^pP][^dD][^fF])$', but I'm experiencing similar results.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

2

An easier way to do this is by using the -notmatch operator instead of -match. This allows you to simply write

$s -notmatch '\.pdf$'

Alternatively you could also use -notlike with wildcards:

$s -notlike '*.pdf'
Joey
  • 344,408
  • 85
  • 689
  • 683
0

To reject matches that end with .pdf only, you can use this regex,

^(?!.*\.pdf$).*$

Which has a negative look ahead (?!.*\.pdf$) which rejects the match if string ends with .pdf

Here are the results of your commands,

PS C:\Users\user> "test.txt" -match '^(?!.*\.pdf$).*$'
True
PS C:\Users\user> "test.docx" -match '^(?!.*\.pdf$).*$'
True
PS C:\Users\user> "test.pdf.txt" -match '^(?!.*\.pdf$).*$'
True
PS C:\Users\user> "test.pdf" -match '^(?!.*\.pdf$).*$'
False
PS C:\Users\user> "test.pdd" -match '^(?!.*\.pdf$).*$'
True
PS C:\Users\user> "test.pdx" -match '^(?!.*\.pdf$).*$'
True

Your usage of negated character [^.pdf] only works for negating certain characters in a string and is not able to reject a sequence of characters like pdf for which you need to use negative look ahead the way I have used.

And .? means any character zero or once which is not the correct thing you wanted.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36