我正在尝试从 Twitter 获得的一些字符串中删除 url。 我使用的代码是:
test.replaceAll("http.*?\\s", ""));
问题是,当我尝试时,有些字符串不起作用,例如:
String cuatro = ("#TodosPorKiKi El plan de flia solidaria No Puede exceder los 6meses. La justicia de Santa Fe lo abandonó 2 años y ahora\r\n" +
"Quieren hacerse los legalistas\r\n" +
"#Verguenza Respete los derechos del niño @MiguelLifschitz @DataLifschitz https//t/MUY0bj2qMT");
output:
我不知道为什么对于某些文本它有效而对于其他文本则无效
最佳答案
正则表达式中的 .*?
将寻找一个最小区域,但实际上没有,因此它将无法在后面找到空格,这不是好方法去做这件事
您需要关注 2 个简单的属性
- 网址以
http
开头 - 网址不包含
空格
因此您的正则表达式可以是:http\S*
(http 后跟多个非空格 字符)
String cuatro = ("#TodosPorKiKi El plan de flia solidaria No Puede exceder los 6meses. https//t/MUY0bj2qMT" +
" La justicia de Santa Fe lo abandonó 2 años y ahora\r\n" +
"Quieren hacerse los legalistas\r\n https//t/MUY0bj2qMT" +
"#Verguenza Respete los derechos del niño @MiguelLifschitz @DataLifschitz https//t/MUY0bj2qMT");
String cleaned = cuatro.replaceAll("http\\S*", "");
System.out.println(cleaned); // I added multiple url in the String, for you can see it remvoes all
关于java - 使用java从文本中删除url时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304866/