-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
  • 55,989
  • 15
  • 126
  • 162
Jacky
  • 11
  • 1
  • 1
  • 2
    Did you make any attempt to do this yourself? The java.util.regex.Pattern javadoc seems to be pretty clear on how to use the metacharacters. – nanofarad Jun 30 '15 at 20:39
  • 1
    You should get some pointers from https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html – beresfordt Jun 30 '15 at 20:39
  • `(?:[all special characters](?<![\d()| -]))+` is one way. The ways this can be expressed is endless.. –  Jun 30 '15 at 20:47
  • Try [^()|\- a-zA-Z0-9] – Vall0n Jun 30 '15 at 20:50
  • Thanks... But I have got another combination [^0-9\\s\\|\\(\\)\\-] – Jacky Jun 30 '15 at 20:56
  • @ShellFish: **Every** regex question is a possible duplicate of that one; that's why they closed it. ;) – Alan Moore Jul 01 '15 at 02:10
  • No, only incredibly basic ones are. Posts that don't show effort and are solvable with minimal effort and a single read through that guide are dupes. – ShellFish Jul 01 '15 at 06:39

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
  • 6,999
  • 4
  • 31
  • 48
  • 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