Use regular expressions to replace:
Code snippet:
String documentTxt = EntityUtils.toString(entity,"gbk");//Get data
documentTxt=documentTxt.replaceAll("[//t//n//r]", "");//Remove carriage returns and line feeds in the content area
Note: replaceAll of String class has regular replacement function. /t is tab/n is line feed/r is carriage return
Java regular use:
Example method:
Copy the code code as follows:
public void parseTxt(String content){
Pattern p = Pattern.compile(Config.articlePtn);
Matcher matcher = p.matcher(content);
while(matcher.find()){
System.out.println(matcher.group(1));
}
}
Note: Just remember the Pattern class, its static method compile parses a regular expression to generate a Pattern object.
Then use the model to match the string, get a Matcher, and traverse all matches through the find method of the matcher.
group is the group in the regular expression, and () expression. group(0) is the original string, gourp(1) is the first matched group...that is, the index of the matched group starts from 1.