文章内容关键字或短语替换

阅读更多
分析的源文件内容:
 


利用正则表达式处理:
 
  
String p1 = "^()$";
        String p2 = "^()$";
        String p3 = "^( list = Files.readAllLines(Paths.get("/home/bmk/testreport/report-files.xml"));
            if (list.size() > 0) {
                StringBuilder xmlString = new StringBuilder();
                Map uuidMap = new HashMap<>();
                List newContent = new ArrayList<>();
                list.stream().forEach(content -> {
                    Matcher fileMatcher = filePatter.matcher(content);
                    if (fileMatcher.lookingAt()) {
                        String dataUuid = fileMatcher.group(3);
                        String fileUuid = fileMatcher.group(6);
                        String newUuid = UUID.randomUUID().toString();
                        uuidMap.put(fileUuid, newUuid);
                        content = content.replace(dataUuid, UUID.randomUUID().toString());
                        content = content.replace(fileUuid, newUuid);
                        newContent.add(content);
                    }
                    Matcher folderMatcher = folderPatter.matcher(content);
                    if (folderMatcher.lookingAt()) {
                        String fileUuid = folderMatcher.group(3);
                        String newUuid = UUID.randomUUID().toString();
                        content = content.replace(fileUuid, newUuid);
                        uuidMap.put(fileUuid, newUuid);
                        newContent.add(content);
                    } else {
                        newContent.add(content);
                    }
                });
                newContent.stream().forEach(content -> {
                    Matcher pidMatcher = pidPatter.matcher(content);
                    if (pidMatcher.lookingAt()) {
                        String pid = pidMatcher.group(4);
                        if (uuidMap.containsKey(pid)) {
                            content = content.replace(pid, uuidMap.get(pid));
                        }
                    }
                    xmlString.append(content + "\r\n");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

你可能感兴趣的:(正则表达式)