开发者

Scanner Delimiters, Reg. Expression Problem

开发者 https://www.devze.com 2023-03-13 04:10 出处:网络
I have a small Problem using reg. expressions with scanner here\'s the code: String name; Pattern p = Pattern.comp开发者_开发百科ile(\"\\\\s+|\\\\W+|\\\\_+\");

I have a small Problem using reg. expressions with scanner

here's the code:

String name;
    Pattern p = Pattern.comp开发者_开发百科ile("\\s+|\\W+|\\_+");
    ArrayList<String> reay = new ArrayList<String>(1000);
    try {
        Scanner asdf = new Scanner(new File(s)).useDelimiter(p);
        while (asdf.hasNext()) {
            name = asdf.next();
            reay.add(name);

        }
        asdf.close();
    }

and the resulting array (using a lot of non-word chars in the text file):

[arst, , tdnxc, , rst, , arst, , arst, wfp, arst, , arst]

not sure what I'm missing and why I get whitespace entries in my array


Your regex matches one or more whitespaces, then OR one or more nonwords, then OR one or more underscores.

So for an input of " $_" it will say, space is a match! Capture what's before next delimiter... $ is a match! Return empty string between space and $. Capture what's before next delimiter.. _ is a match! return empty string between $ and _.

I think you meant for your delimiter to be:

[\\s\\W_]+


Maybe because you use whitespace OR non-word characters OR underscores as delimiter. What shall happen if you e.g. have several of them mixed straight after one another?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号