Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

What string pattern will match an explicit asterisk (*) or at-sign (@)? The obvious example, StringMatchQ["*", "\*"], returns False. I can't find any mention of escaping them in string patterns, only in RegularExpressions, and that was in the advanced tutorial.

share|improve this question

1 Answer 1

Yes, you need to escape a * character in a string pattern because it's a wildcard. To escape it, just prepend a backslash to it. Remember that to insert a backslash in a Mathematica string, you need to type two backslashes:

StringMatchQ["*", "\\*"]

(* => True *)
share|improve this answer
    
Come to think about it, this is pretty deep stuff. In a normal string, an asterisk isn't a special character that needs to be escaped, so "*" is just the character with character code 42, and "\*" is a single character with code 63432. "\\*" equals {92, 42}, and only because it is used in a string pattern (where * is special) it is interpreted as an escape sequence and equals the literal "*" (42). Note that "*"=="\\*" yields False (whereas "*"=="*" yields True). –  Sjoerd C. de Vries May 23 '13 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.