我有一个.txt文件。实际上,我是通过读取URL和转换HTML文件得到的。我的.txt文件包含如此多的特殊字符。我只想保留英文单词。我用过,
`String result = listOfWords.replaceAll("[^a-zA-Z]+"," ");`但是,输出会替换一些特殊字符,如LRB、LSB、RSB、LRB、RRB等。
例如:
Eleanor (2008), Mathematicsrecent years. (TOP500 2006)^ Agatha C. Hughes (2000). Systems, Experts, and Computers. MIT Press. p. 161. ISBN 978-0-262-08285-3. The experience of SAGE helped make possible the first truly large-scale commercial real-time network: the SABRE computerized airline reservations system
替换后的结果为,
Eleanor LRB RRB Mathematicsrecent years LRB TOP RRB Agatha C Hughes LRB RRB Systems Experts and Computers MIT Press p ISBN The experience of SAGE helped make possible the first truly large scale commercial real time network the SABRE computerized airline reservations system
如何解决这里的代码输入这个问题?
发布于 2016-06-01 05:04:06
您的正则表达式替换只会增加较大的间隙(空格)。LRB (左圆括号)和RRB (右圆括号)很可能来自您在regex替换之前所做的任何处理。如果您不关心特殊字符,您可能应该删除它们:
String result = listOfWords.replaceAll("[^a-zA-Z]+","");正如@Emalka提到的,NLTK是quick 'HOWTO'的一个很好的来源。他们正在使用BeautifulSoup。因为你的问题谈到了Java,所以有一个很好的答案是使用Jsoup的here on SO。
https://stackoverflow.com/questions/37536068
复制相似问题