输入文本的文本文件的内容具有内容如下:验证使用正则表达式
TIMINCY ........许多任意字符含。空格和制表符
细节........许多任意字符incl。空格和制表符
细节........许多任意字符incl。空格和制表符
细节........许多任意字符incl。空格和制表符
。
。
。 (任何数量的行包含DETAILS)
TIMINCY ........许多任意字符incl。空格和制表符
细节........许多任意字符incl。空格和制表符
细节........许多任意字符incl。空格和制表符
细节........许多任意字符incl。空格和制表符
。
。
(等等)
Q:我需要使用正则表达式来验证文件,以便如果该文件的内容是不 按照相对于上述然后我可以抛出CustomException给出的图案。
请让知道,如果你能帮助。任何帮助热烈赞赏。
String patternString = "TMINCY"+"[.]\\{*\\}"+";"+"["+"DETAILS"+"[.]\\{*\\}"+";"+"]"+"\\{*\\}"+"]"+"\\{*\\};";
Pattern pattern = Pattern.compile(patternString);
String messageString = null;
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(curracFile.toPath(), charset)) {
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(NEWLINE_CHAR_SEQUENCE);
}
messageString = builder.toString();
} catch (IOException ex) {
LOGGER.error(FILE_CREATION_ERROR, ex.getCause());
throw new BusinessConversionException(FILE_CREATION_ERROR, ex);
}
System.out.println("messageString is::"+messageString);
return pattern.matcher(messageString).matches();
但它是正确的文件返回FALSE。请帮我用正则表达式。
* CORRECTION ::,我使用的模式是:字符串patternString = “[” + “TMINCY” + “[] \\ {* \\}” + “” +“[ “+” 详情 “+” \\ {* \\} “+”[。]; “+”] “+” \\ {* \\} “+”] “+” \\ {* \\}; “;而且,错过了提到以下内容:private static final String NEWLINE_CHAR_SEQUENCE =“;”; – saiD
编辑您的帖子,而不是写评论。你真的需要使用REGEX吗?你为什么不在循环中逐行阅读? – jeanr
强制性:https://xkcd.com/1171/ – jdv