简体   繁体   English

Java Regex除空格外的所有非单词字符

[英]Java Regex all non word characters except whitespace

This has Probably been asked before, but i want to split a string at every non word character except the white space in java. 可能以前有人问过这个问题,但是我想在每个非单词字符处拆分一个字符串,除了Java中的空格。 i do not have experience with regex in general and the wiki doesn't really help. 我一般没有正则表达式的经验,而Wiki并没有真正的帮助。

I've tried it with this: "[\\\\W][^\\\\s]" but that did not help. 我已经试过了: "[\\\\W][^\\\\s]"但这没有帮助。

Edit : how the String is read out of the file 编辑 :如何从文件中读取字符串

StringBuilder sb = new StringBuilder();

        Scanner sc = new Scanner(getResources().openRawResource(R.raw.answers));

        try
        {
            while (sc.hasNext())
            {
                sb.append(sc.next());
            }
        } finally
        {
            sc.close();
        }

You can split using this regex: 您可以使用此正则表达式进行拆分:

String[] tok = input.split( "[\\W&&\\S]+" );

This will split on any non-word that is also a non-space character hence leaving aside space characters for split. 这将在也是非空格字符的任何非单词上进行分割,因此留出空格进行分割。

Check Character classes in Java Pattern reference . Java Pattern Reference中检查Character类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM