This one is particularly powerful when used in conjunction with other regular expressions, but can be used to great effect on its own.
The dot acts as a limited wildcard that lets you match with any alternative for one character only. This post will take you through a few examples of its use, some common mistakes and a practical tip or two.
A dot will match anything in regular expressions
A dot will match any character, including special characters, that it comes across in regular expressions. It will match normal characters like letters and numbers, but also the other characters you will see from time to time that normally need to be escaped using the backslash. The dot will match question marks, other dots, plus signs, etc.
Imagine you wanted to match the words: rare, hare, fare, care and mare.
The regex for this would be:
I use fullstops at work quite frequently in content reports where i want to match multiple publications. Our site uses a structure where each different language is prefixed by an iso code for the country, for example ‘/fr/index.html’ for france and ‘/de/index.html’ for Germany.
Our English language publication is missing this layer of structure, so it is simply ‘/index.html’.
When i want to look at the language publications, but not the english one, my regex is as follows:
The dot matches any character, including other dots, so you will rarely have a problem when using a dot as a normal character rather than a regular expression. It’s
easy as you become more proficient at regex to get lazy and not construct your regex as precisely as you could as there’s a good chance in many cases it will match anyway.
This is especially true of the dot.
It’s a great idea to make a point of always escaping the dot using a backslash where you are using it as a character rather than code. Have another look at the example above to see how I have done this. It is a simple habit to form, and it removes the possibility of your dots matching things you didn’t intend.
Was this post useful? Have you got any great examples of using dots in Regex? Leave a comment and let me know!
Links to the rest of the series:
RegEx 1: Introduction
RegEx 2: Pipe
RegEx 3: Brackets
RegEx 4: Question Mark
RegEx 5: Backslash
RegEx 6: Plus Sign
RegEx 7: Dot – You are here!
RegEx 8: Star
RegEx 9: Dot Star
RegEx 10: Caret
RegEx 11: Dollar Sign
RegEx 12: Square Brackets
RegEx 13: 5 Great Places to use RegEx