Skip to main content

string manipulation - Why is StringMatchQ["IP1", "IP"] returning False?


Maybe there is a knot in my head, but I would expect that


StringMatchQ["IP1", "IP"]

should return True (which it does not) just as



StringFreeQ["IP1", "IP"]

returns False (which it does). What am I missing?



Answer



StringMatchQ tests the pattern, and "IP1" doesn't match the pattern "IP", it does match the pattern ___~~"IP"~~___, but that's a different pattern! Similarly you could write your pattern as StringMatchQ["IP1", "IP*"]


StringFreeQ however doesn't just test the entire string, it tests every substring, so in effect the ___~~pattern~~___ is added by the function itself to your pattern.


Comments