我有一个.txt文件。实际上,我是通过读取URL并转换HTML文件得到的。我的.txt文件包含很多特殊字符。我只想保留英文单词。我用了,

`String result = listOfWords.replaceAll("[^a-zA-Z]+"," ");`


但是,输出将某些特殊字符替换为LRBLSBRSBLRBRRB之类的单词。

例如:

Eleanor (2008), Mathematics


recent 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    Mathematics


recent 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

如何在这里解决输入代码这个问题?

最佳答案

您的正则表达式替换只会增加较大的差距(空格)。 LRB(左圆括号)和RRB(右圆括号)很可能来自您在更换正则表达式之前所做的任何处理。如果您不关心特殊字符,则应该删除它们:

String result = listOfWords.replaceAll("[^a-zA-Z]+","");


如@Emalka所述,NLTK是quick 'HOWTO'的良好来源。他们正在使用BeautifulSoup。当您的问题谈论Java时,使用here on SO可以很好地回答Jsoup

关于java - 从.txt文件中删除特殊字符会在Java中提供`LRB`,`LSB`,`RSB`,`LRB`,`RRB`等。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37536068/

10-10 21:41