在java中替换\和换行替换

时间:2017-04-24 23:38:42

标签: java replaceall

我正在编写代码来检测文件中的错误关键字。以下是我遵循的步骤:

  1. 使用StreamTokenizer进行令牌化
  2. 使用模式匹配器查找匹配项

    while(streamTokenizer.nextToken() != StreamTokenizer.TT_EOF){
                if(streamTokenizer.ttype == StreamTokenizer.TT_WORD) {
                    String token = streamTokenizer.sval.trim().replaceAll("\\\\n", "")
                    final Matcher matcher = badKeywordPattern.matcher(token)
                    if(matcher.find()) { // bad tokens found
                         return true;
                    }
                }
    }
    
  3. 完成

    String token = streamTokenizer.sval.trim().replaceAll("\\\\n", "")以匹配使用\跨越多行的令牌。例如:

    bad\ token

    然而,替换不起作用。有什么建议?还有其他方法吗?

3 个答案:

答案 0 :(得分:1)

假设您要删除位于该行末尾的所有replaceAll("\\\\\\R","")以及行分隔符,您可以使用\

  • 要在正则表达式中表示replaceAll\使用的是{}},我们需要使用另一个\\来转义它,这会留下\。但是由于"\\\\"在字符串文字中也很特殊,我们需要使用另一个反斜杠再次转义它们,这使我们留下\R

  • 从Java 8开始,我们可以使用"\\R"(需要将\写成\r,因为\n需要转义)来表示行分隔符,例如\r\n {{ 1}}或 private void createOneToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) { OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource); ontoClass.addSuperClass(allValuesFromRestriction); OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1); ontoClass.addSuperClass(minCardinalityRestriction); } private void createZeroToMany(OntModel ontoModel, OntClass ontoClass, OntProperty prop, Resource resource) { OntClass allValuesFromRestriction = ontoModel.createAllValuesFromRestriction(null, prop, resource); ontoClass.addSuperClass(allValuesFromRestriction); OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0); ontoClass.addSuperClass(minCardinalityRestriction); } private void createZeroToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) { OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 0); ontoClass1.addSuperClass(minCardinalityRestriction); OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1); ontoClass1.addSuperClass(maxCardinalityRestriction); } private void createOneToOne(OntModel ontoModel, OntClass ontoClass1, OntProperty prop, OntClass ontoClass2) { OntClass minCardinalityRestriction = ontoModel.createMinCardinalityRestriction(null, prop, 1); ontoClass1.addSuperClass(minCardinalityRestriction); OntClass maxCardinalityRestriction = ontoModel.createMaxCardinalityRestriction(null, prop, 1); ontoClass1.addSuperClass(maxCardinalityRestriction); } 对。

答案 1 :(得分:1)

如果我理解正确,你不想使用正则表达式(这是String.replaceAll所做的),只需用String.replace替换文字字符串,并使用少一个反斜杠:

String token = streamTokenizer.sval.trim().replace("\\\n", "")

答案 2 :(得分:0)

基于@Pshemo answer,向您展示\& \n在正则表达式中呈现,如上所述here。你可以这样做:

String[] tkns = streamTokenizer.sval.trim().split("\\\\\\R"); // yourString = "bad\\\ntaken"
StringBuffer token= new StringBuffer();
for (String tkn : tkns)
{
    token.append(tkn);
    //System.out.println(tkn);
}
//final Matcher matcher = badKeywordPattern.matcher(token)