0

I know this a duplicate of a question with almost the identical name, however, I can't get it to work in Android what so ever!

I am trying this: Regex to Match Symbols:

public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\\";'<>?,.\/]");

However, this isn't working. Does anyone know the correct method of applying this pattern?

P.S. Complete noob at Regex. :D

From here originally - Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./

Error Message: Syntax error on token(s), misplaced construct(s)

UPDATE: Added extra backslashes...fixed a lot of em, now gets error from ; onwards. Using Eclipse.

Community
  • 1
  • 1
JosephGarrone
  • 4,081
  • 3
  • 38
  • 61

3 Answers3

4

I think your problem is the "

public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]");
                                                                  ^

it is ending your string, so you should escape it. Also you need to remove the backslash before the slash, it is no special character.

public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");

OK, once more, you wanted to match the backslash, not to escape the slash, then we end up here:

public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");

now it is the same answer, than jdb's, so +1 to him for being quicker.

stema
  • 90,351
  • 20
  • 107
  • 135
  • Also need to double escape `\[` and `\]`. – Rohit Jain Jan 25 '13 at 09:10
  • @stema what's the need of escaping `;` ? – Naveed S Jan 25 '13 at 09:12
  • Still not getting any lovin' after copy-pasting your expression. - `Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )` – JosephGarrone Jan 25 '13 at 09:13
  • you need to esape '()' '.' '?' '+' '*' '|' '{}' '[]' as they are all special characters in regexp, and you have to use double backslashes in java to escape a character – Adam Monos Jan 25 '13 at 09:15
  • @AdamL.Mónos every special character except `[`, \ and `]` is the character itself inside character class. – Naveed S Jan 25 '13 at 09:17
  • @Asryael, I updated my expression. I am not sure about the escaping, I think the regex special characters need 2 backslash and the string special characters only one. – stema Jan 25 '13 at 09:17
  • @Asryael, so I started up eclipse and tested the expression and fixed it once more, you need also to remove the escaping from the slash. It is no special character in Java regex and Java strings claims an "invalid escape sequence" – stema Jan 25 '13 at 09:36
3

How about that?

Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
jdb
  • 4,419
  • 21
  • 21
1

In a character class, only [ and ] have special meaning, so you need to escape them. Plus in Java, you need to escape with an extra backslash. That's the problem specifically with Java. So, you need to use \\[ and \\]. And yes, you need to escape " with single backslash, in a string literal.

Apart from that, a hyphen when used somewhere in the middle, has also a special meaning. If you want to match a hyphen, you need to use it at the ends.

Rest of the characters, don't need to be escaped. They are just ordinary characters.

So, your pattern should be like this: -

Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");

And if you want to match backslash (\) also, then use this: -

Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Thank you for your answer it works for the characters listed, however you are missing the backslash character if I am correct. – JosephGarrone Jan 25 '13 at 09:21
  • \ should be escaped inside character class also. – Naveed S Jan 25 '13 at 09:22
  • @Asryael.. Yeah added that. I thought you were using it for escaping `/` . But to match a *backslash*, you need to use four backslashes . Two for Regex, and two for Java. – Rohit Jain Jan 25 '13 at 09:23