20.3. Escaping wild cards

[<<<] [>>>]

Sometimes you want a wild card character or joker character to match only itself. For example you want to match the string "13*52" to the pattern two numbers separated by a star. The problem is that the star character is a wild card character and therefore "#*#" matches any string that starts and ends with a digit. But even if that is not a problem more issues still may arise. A * character matches one or more characters, and therefore "#*#" will indeed match "13*52". The problem is, when we want to use the sub-strings.

Const nl="\n"
a="13*52" like "#*#"
print joker(1)," ",joker(3),nl
a="13*52" like "#~*#"
print joker(1)," ",joker(2),nl 

will print

1 52 13 52

The first # character matches one character, the * character matches the sub-string "3*" and the final # matches the number 52. (We will soon explain the details why that is in the section @#ref{Ambiguous matching}.)

The solution is the pattern escape character. The pattern escape character is the tilde character: ~.

Any character following the ~ character is treated as normal character and is matched only by itself. This is TRUE for any normal character, for wild card characters; joker characters; for the space and finally for the tilde character itself. The space character following the tilde character matches exactly one space characters.

Later you will learn that "#*#" may also serve the purpose, when the role of the wild card character * is changed.

[<<<] [>>>]