Scanner Delimiters, Reg. Expression Problem
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?
精彩评论