Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell RegEx match negative lookbehind at start of string

I'm currently working on a WSUS-Update automization and I'd like to get all Updates that don't start with "(German|English) Language Pack".

This is what I got so far:

[regex]$reg = "(?<!German|English|English \(United States\)) Language Pack"
$LanguagePacks = $updates.Where({ $_.Title -match $reg })

That works, but I also get updates like: Windows Internet Explorer 9 Language Pack for Windows 7 for x64-based Systems

But I also want to get Updates in the following syntax: [Language] Language Pack e.g. Finnish Language Pack

So I tried to use the '^' anchor to determine the start of the String

[regex]$reg = "^(?<!German|English|English \(United States\)) Language Pack"
$LanguagePacks = $updates.Where({ $_.Title -match $reg })

But in this case the result is empty :(

like image 370
Eldo.Ob Avatar asked Oct 11 '25 14:10

Eldo.Ob


1 Answers

You may use

(?<!^(?:German|English(?:\s+\(United States\))?)\s*)Language Pack

See the regex demo

It matches Language Pack if it is not preceded with German, English, or English (United States) at the start of the string.

Details

  • (?<! - start of a negative lookbehind that fails the match if, immediately to the left of the current location, there are patterns:
    • ^ - start of string
    • (?: - start of an alternation non-capturing group:
      • German - a literal substring
      • | - or
      • English - a literal substring
      • (?:\s+\(United States\))? - an optional non-capturing group matches 1 or 0 occurrences of:
        • \s+ - 1 or more whitespaces
        • \(United States\) - a literal (United States) substring
    • ) - end of the alternation group
    • \s* - 0+ whitespaces
  • ) - end of the lookbehind
  • Language Pack - a literal substring
like image 168
Wiktor Stribiżew Avatar answered Oct 14 '25 13:10

Wiktor Stribiżew