由于从 PDF 进行文本推断,我需要修复一些错误。 我需要替换每种形式的字符串,如下所示:
String example="the sun was shin- ing and the sky bl- ue";
形式为:
String fixxed="the sun was shining and the sky blue";
我不是正则表达式方面的专家,我尝试这样做,但这是错误的。
String pattern="([\\w])+([\\-])+([\\s])";
String fixxed = text.replaceAll(pattern, "$1");
一个重要的规范,如果“-”之前的字符是字母(不是空格也不是数字),我只需替换子字符串。
最佳答案
按如下方式进行:
public class Main {
public static void main(String[] args) {
String example = "the sun was shin- ing and the sky bl- ue";
example = example.replaceAll("\\-\\s+", "");
System.out.println(example);
}
}
输出:
the sun was shining and the sky blue
关于java - 如何在java中查找和替换某些字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62259726/