java - 标记化后列表中的空白

标签 java tokenize

我有一个单词列表,在标记化之后,我的列表中出现了一些我实际上不想要的空白。请提出建议...

示例字符串

String str = "3) type an \"l\" in the search field (\"bl\")"+
"4) startHistorySearch()'s previous result contains [\"blah\", \"baaa\", \"bloop\", \"bzzz\"], Satchel filters this down to [\"blah\", \"bloop\"] to match the new \"bl\" search string"+
and so on.....

这是代码和输出片段

String[] splitString = (EXAMPLE_TEST.split("[\\[\\],\\'\"  \\(\\)\\{\\}\\*\\.]"));

输出

nsIAutoCompletResult, , no, , Simple, , , , so, the, QI, fails, , historyResult

在某些地方我看到这样的......

finds, 1, entry, , , blah, , , , search-suggestions, finds, , baaa, , , , bloop, , , , bzzz, , , the, autocompete, menu, shows, these, in, order, with, a, divider, between, , blah, , and, , baaa, , 3, , type, , l, , in, the, search, field, , , bl, , 4, , startHistorySearch, , , s, previous, result, contains, , , blah, , , , baaa, , , , bloop, , , , bzzz, , , , Satchel, filters, this, down, to, , , blah, , , , bloop, , , to, match, the, new, , bl, , search, string5, , nsSearchSuggestions, s, onReadyState, , , change, is, called, with, updated, search,

最佳答案

只需在表达式中添加一个 + 即可,以免在两个标记之间拆分。您还可以稍微简化表达式,不必转义字符类中的所有这些字符:

String[] splitString = (EXAMPLE_TEST.split("[\\[\\],'\" (){}*.]+"));

关于java - 标记化后列表中的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13173134/

相关文章:

java - 在java中用空格标记一个字符串

postgresql - 当名称可以是任何语言时,如何按名称索引 postgres 表?

java - 无法通过java程序在mac os中运行shell脚本

java - 尝试将文本文件元素添加到数组列表中

java - 更改 Activity 时如何停止 PhoneStateListener

java - 如何在 Java 中实现字符串格式化机制?

iOS:如何在使用 componentSeparatedByCharactersInSet 时维护分隔符

java - 使用复杂的 TableCellRenderer 在 JTable 中格式化来自 JDBC 的数据

java - 如何在Java中初始化Google Protocol Buffer 时间戳?

python 字符串标记化 - 自定义词法分析器?