Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of [...] regex?

Tags:

java

regex

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.

like image 971
codegasmer Avatar asked May 06 '15 07:05

codegasmer


1 Answers

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 [.].

like image 66
Maroun Avatar answered Oct 13 '22 01:10

Maroun