I was making a RegEx using the regex101 tool and read in the explanation field
[.]
- the literal character .
[\.]
- matches the character .
literally
I get lost between "literal character" and "character literally". What is the difference between these two?
A literal character or matching something literally refers to specifying an actual character in the text: for instance, a to match a , as opposed to a character class such as \w that could also match a .
matches an optional character . or - . Although dot ( . ) has special meaning in regex, in a character class (square brackets) any characters except ^ , - , ] or \ is a literal, and do not require escape sequence.
Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
There is no difference. Sorry, I take that back. The only difference the words that Firas Dib, the author of regx101, chose to explain various tokens.
A literal
character or matching something literally
refers to specifying an actual character in the text: for instance, a
to match a
, as opposed to a character class such as \w
that could also match a
.
You can match a literal period in either of these three ways:
\.
[.]
[\.]
Which Option is Better?
\.
. Some people will say that using a character class is less optimal, but on modern processors it makes no difference. You pick.[\\.]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With