Search code examples
regexposix

What is the purpose of [.] in regular expressions?


I found [.] in a regular expression on manpage of notmuch:

notmuch search 'from:"/bob@.*[.]example[.]com/"'

It seemed to be useless because brackets are for list but have only one character, but finally I learned it matches a literal dot.

Then, why they use it rather than \.? Are there any advantages on this expression?


Solution

  • At first I thought that this is to avoid double escaping but on further consideration I think this is because a dot in a character set ([]) is treated differently than normal. It makes sense that in a character set a dot only matches a literal dot, the whole point is to match a specific set of characters so having a wildcard in the set doesn't make sense.

    So [.,;:] may be used to match punctuation marks.

    Once you take that into account it's obvious that [.] just matches dot.

    Whether to use \. or [.] is left as an aesthetic decision.