I'm trying to create a regex that works with the -match
operator. The following already works:
# Exact match
$string1 = [Regex]::Escape('C:\Fruit\kiwi')
$regex = [regex] '^{0}$' -f $string1
'C:\Fruit\kiwi' -match $regex
# Match where trail is allowed ( -like 'c:\folder*')
$string1 = [Regex]::Escape('C:\Fruit\kiwi')
$regex = [regex] '^{0}' -f $string1
'C:\Fruit\kiwi\other folder' -match $regex
Now we're trying to have a match when there is something between two strings, but this fails:
# Match in between strings
$string1 = [Regex]::Escape("C:\Fruit")
$string2 = [Regex]::Escape("\kiwi")
$regex = [regex] '(?is)(?<=\b{0}\b)+?(?=\b{1}\b)' -f $string1, $string2
'C:\Fruit\d\kiwi' -match $regex
According to the docs it says:
- '*' matches 0 or more times
- '+' matches 1 or more times
- '?' matches 1 or 0 times
- '*?' matches 0 or more times, but as few as possible
- '+?' matches 1 or more times, but as few as possible
So I was expecting that anything between C:\Fruit
and \kiwi
would result in true
but this is not the case. What are we doing wrong? We're only after true false, because in the end we will glue these pieces together like regex1|regex2|..
.