I am new to regex going through the tutorial I found the regex [...]
says Matches any single character in brackets.
. So I tried
System.out.println(Pattern.matches("[...]","[l]"));
I also tried escaping brackets
System.out.println(Pattern.matches("[...]","\\[l\\]"));
But it gives me false
I expected true
because l
is inside brackets.
It would be helpful if anybody clear my doubts.
Characters that are inside [
and ]
(called a character class) are treated as a set of characters to choose from, except leading ^
which negates the result and -
which means range (if it's between two characters). Examples:
[-123]
matches -
, 1
, 2
or 3
[1-3]
matches a single digit in the range 1 to 3[^1-3]
matches any character except any of the digits in the range 1 to 3.
matches any character
[.]
matches the dot .
If you want to match the string [l]
you should change your regex to:
System.out.println(Pattern.matches("...", "[l]"));
Now it prints true
.
The regex [...]
is equivalent to the regexes \.
and [.]
.
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