2

Below is my code which allows special characters, numbers, characters(upper and lower) .This program is working fine. My issue with square brackets.

public class MatchingSpecificCharacters {
    public static void main(String[] args) {

        String reg = "[A-Za-z0-9!$-~`?/@#%^*&()_+=<>.,';:|\" ]*";
        String line = "as[]d";

        System.out.println(line.matches(reg));


    }
}

output

true

In the program, I have used [] brackets to enclose all the characters, numbers , special characters . I have not used extra square brackets to allow as special charters but the program is allowing it. Can anyone tell me why it is allowing square brackets.Correct me If I am wrong.

rajeev pani..
  • 5,387
  • 5
  • 27
  • 35

2 Answers2

5

You should escape the hyphen.

String reg = "[A-Za-z0-9!$\\-~`?/@#%^*&()_+=<>.,';:|\" ]*";
                           ^

or place it at the end

String reg = "[A-Za-z0-9!$~`?/@#%^*&()_+=<>.,';:|\" -]*";

This is what your regex matches (as instead of a hyphen, you defined a range from $ till ~):

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • The keyboard you have given, is not regular keyboard.Can give complete picture of the keyboard.(I don't know whether it is a keyboard). Please specify, it will be helpful to others also. – rajeev pani.. Sep 01 '15 at 06:50
  • 1
    It is not a keyboard, it is an [ASCII table](http://www.asciitable.com/) which is a part of the [Unicode code point table](http://unicode.org/charts/). Regex is using Unicode code point table to resolve character ranges in [character classes](http://www.regular-expressions.info/charclass.html). – Wiktor Stribiżew Sep 01 '15 at 06:54
1

Since you are using a hyphen, you can either escape it or place it as the first or last character in the range:

[-a-z] or [a-z-]

Otherwise, [A-Za-z ... $-~ ... \" ] is trying to match all the given characters plus everything in between $ and ~, that you can visually see in stribizhev's good answer.

See also How to match hyphens with Regular Expression?:

  • [-] matches a hyphen.
  • [abc-] matches a, b, c or a hyphen.
  • [-abc] matches a, b, c or a hyphen.
  • [ab-d] matches a, b, c or d (only here the hyphen denotes a character range).
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Never escape hyphen as it is not special character. When it is used in a range put it at beginning or end to avoid matching the range format. – MaxZoom Aug 10 '15 at 18:36