Author Posts

September 30, 2014 at 5:54 am

I'm trying some regular expression and I found one interesting thing.
This is TRUE:

>_ "Zilinec" -match "Z*ec"
True

which is OK.

But when I limit "Z" only for beggining of the string:

>_ "Zilinec" -match "^Z*ec"
False

it gives me False. Why? 🙂

September 30, 2014 at 6:01 am

In regex, * is a quantifier that means the previous pattern must be repeated zero or more times. In this case, your pattern states that the string must begin with zero or more Z characters (which it does), followed by the letters "ec" (which it does not.) If you want that * to match any number of characters _after_ the Z (which is how it would work in a wildcard pattern passed to the -like operator), you need to use another special regex character, the period. This matches any single character, and then you can put a * after it to mean (any number of other characters):

"Zilinec" -match "^Z.*ec$"

(The $ character at the end of the pattern means that must be the end of the string, so it wouldn't match "Zilinec Something Else")

October 1, 2014 at 12:25 am

Thank you for a explanation 🙂 I forgot about ".".