-6

I need to write a java pattern to identify all special characters except "0123456789" or "(" or ")" or "|" or "-" or " ".

Can somebody help me to get an answer?

I wanted to use Pattern.compile and pattern.matcher to find out this.

nhahtdh
  • 54,546
  • 15
  • 119
  • 154
Jacky
  • 11
  • 1
  • 1

1 Answers1

0

If you want to find all characters separately you can use this pattern-

/[^ 0123456789()|\-]/g

Or if you want to find blocks of consecutive characters, you should use this-

/[^ 0123456789()|\-]+/g

This will identify all characters except those inside [^(*this part*)]

By the way, if you want to research further about regex pattern http://regexr.com/ will be very helpful. There are quick references and tutorials to help you learn the basics of regex quickly.

Dipu
  • 4,886
  • 3
  • 27
  • 46
  • You need to escape that hyphen (`\-`) or move it to the last position: `[^0123456789()| -]`. Both Java and RegExr will treat `[|- ]` as a range, and throw an exception because the end points are listed in the wrong order. But I don't think we have enough information to answer this question yet. For example, what does the author mean by "special characters"? – Alan Moore Jul 01 '15 at 02:27