tags:

views:

105

answers:

3

Hi,

i need a Regex to do this ( catch only real words ):

Inputstring:

hello,[email protected]

Process:

 String theMagicRegEx = "?????";
 String[] arrayvar = asdf.split(theMagicRegEx);

Output (arrayvar) should be this:

hello
sdfsdf
yahoo
email
com

Question: What is the value of theMagicRegEx?

Thanks a lot

+5  A: 

Try \W+ (non-word characters)

streetpc
+1. `str.split("\\W+");` to be exact.
Amarghosh
...but be aware that numbers and underscore (_) are also part of `\w` and therefore there won't be a split in `good4you`, for example.
Tim Pietzcker
How would i integrate these characters aswell?
David
See Tim's answer if you want numbers and underscore to be considered as separators as well
streetpc
+3  A: 

So you want to split the string on non-word characters? There you have the \W for. Also see the java.util.regex.Pattern API for an overview of regex operators.

String[] arrayvar = asdf.split("\\W+");
BalusC
+3  A: 

Try str.split("[\\W\\d_]+");.

This will leave only word characters, no numbers or underscore.

Tim Pietzcker