文章目录
匹配以特定字符开头和结尾的字符串
以“〖SQ0〗〖XXZX-ZXY3\.5mm〗〖XXYX-YXY3\.5mm〗〖WT8\.25F7〗〖BM〗〖WTB1〗〖HT〗〖〗〖SQ0〗〖XXYX-YXY3\.5mm〗〖WTXT〗”开头,以“〖WT〗〖〗〖XXYX-YXY3\.5mm〗〖SQ\+0mm〗〖WT8\.25F7〗〖BM〗”结尾
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GBKFileProcessor {
public static void main(String[] args) {
String filePath = "C:\\Users\\yan\\Desktop\\test.fbd"; // GBK编码文件路径
try {
Charset gbkCharset = Charset.forName("GBK");
String content = new String(Files.readAllBytes(Paths.get(filePath)), gbkCharset);
//正则表达式
Pattern pattern = Pattern.compile("〖SQ0〗〖XXZX-ZXY3\\.5mm〗〖XXYX-YXY3\\.5mm〗〖WT8\\.25F7〗〖BM〗〖WTB1〗〖HT〗〖〗〖SQ0〗〖XXYX-YXY3\\.5mm〗〖WTXT〗.*?〖WT〗〖〗〖XXYX-YXY3\\.5mm〗〖SQ\\+0mm〗〖WT8\\.25F7〗〖BM〗", Pattern.DOTALL);
Matcher matcher = pattern.matcher(content);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
String newContent = sb.toString();
// 将处理后的内容写回文件
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath), gbkCharset)) {
writer.write(newContent);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("处理完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
截取特定字符及其前后第一个括号
package DXH;
public class SubstringExtractor {
public static void main(String[] args) {
String s1 = "示例";
String s2 = "大家好(这是包含示例的括号中的内容)需要被展示";
String result = extractSubstring(s1, s2);
System.out.println(result); // 输出 (hi你好哈哈)
}
public static String extractSubstring(String s1, String s2) {
// 查找 s1 在 s2 中的位置
int index = s2.indexOf(s1);
if (index == -1) {
return ""; // 如果 s1 不在 s2 中,则返回空字符串
}
// 从 s1 开始向左查找第一个左括号 (
int leftParenIndex = s2.lastIndexOf('(', index);
if (leftParenIndex == -1) {
return ""; // 如果没有找到左括号,则返回空字符串
}
// 从 s1 开始向右查找第一个右括号 )
int rightParenIndex = s2.indexOf(')', index);
if (rightParenIndex == -1) {
return ""; // 如果没有找到右括号,则返回空字符串
}
// 确保找到的右括号在左括号之后
if (rightParenIndex <= leftParenIndex) {
return ""; // 如果右括号在左括号之前或者与左括号相同,则返回空字符串
}
// 截取从左括号到右括号的子串,包括这两个括号
return s2.substring(leftParenIndex, rightParenIndex + 1);
}
}
处理字符串并截取以“&”开头,且"&"之后第一个“¥”之间的子串
package DXH;
public class StringExtractor {
public static String fun(String input) {
// 确保输入字符串不为空
if (input == null || input.isEmpty()) {
return "";
}
// 查找开头“&”的位置
int startIndex = input.indexOf("&");
// 查找第一个“¥”的位置
int endIndex = input.indexOf("¥");
// 确保“&”在第一个“¥”之前,并且都被找到
if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) {
// 返回从“&”到第一个“¥”之间的子串(包括“&”和第一个“¥”)
return input.substring(startIndex, endIndex + 1);
}
// 如果找不到符合条件的子串,则返回空字符串
return "";
}
public static void main(String[] args) {
String input = "这是一个测试字符串 &要提取的子串¥还有其他内容¥";
String result = fun(input);
System.out.println("提取的子串是: " + result); // 输出: &要提取的子串¥
}
}
输出
提取的子串是: &要提取的子串¥
删除字符串中指定内容并返回
public static String deleteBracket(String input) {
if (input == null || input.isEmpty()) {
return input; // 如果输入为空或null,直接返回
}
// 使用replaceAll方法删除特定模式的字符串
return input.replaceAll("〖DK〗·", "") // 删除所有 "〖DK〗·"
.replaceAll("〖.*?〗", "");// 删除所有 "〖**〗"
}
在单元格的第n列开头添加【
此时n=3
class ExcelModifier {
public static void main(String[] args) {
String inputFilePath = "C:\\Users\\yan\\Desktop\\新时代学生字典专项(15).xlsx"; // 输入文件路径
String outputFilePath = "C:\\Users\\yan\\Desktop\\新时代学生字典专项(16).xlsx"; // 输出文件路径
try (FileInputStream fis = new FileInputStream(new File(inputFilePath));
Workbook workbook = new XSSFWorkbook(fis);
FileOutputStream fos = new FileOutputStream(new File(outputFilePath))) {
Sheet sheet = workbook.getSheetAt(0); // 获取第一个sheet
for (Row row : sheet) {
Cell cell = row.getCell(2); // 获取第三列(注意:列索引从0开始,所以第三列的索引是2)
if (cell != null && cell.getCellType() == CellType.STRING) {
String oldValue = cell.getStringCellValue();
cell.setCellValue("【" + oldValue);
}
}
workbook.write(fos);
System.out.println("第三列每个单元格的开头添加了“【”符号,已保存到 " + outputFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
第一列的单元格为空,则将第二列对应的单元格内容设置为空
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class DelSecondcellExcelProcessor {
public static void main(String[] args) {
String filePath = "C:\\Users\\yan\\Desktop\\新时代学生字典专项(14).xlsx"; // 输入文件路径
try (FileInputStream fis = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 获取第一个sheet
for (Row row : sheet) {
Cell firstCell = row.getCell(0); // 获取第一列的单元格
Cell secondCell = row.getCell(1); // 获取第二列的单元格
// 如果第一列的单元格为空或内容为空,则将第二列的单元格内容设为空
if (firstCell == null || firstCell.getCellType() == CellType.BLANK ||
(firstCell.getCellType() == CellType.STRING && firstCell.getStringCellValue().isEmpty())) {
if (secondCell != null) {
secondCell.setCellValue("");
}
}
}
// 将修改后的数据写入到新的文件
try (FileOutputStream fos = new FileOutputStream("C:\\Users\\yan\\Desktop\\新时代学生字典专项(15).xlsx")) {
workbook.write(fos);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
删除同一列相邻行的重复值,只保留一个
class deExcelFilter {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("C:\\Users\\yan\\Desktop\\新时代学生字典专项(13).xlsx");
Workbook workbook = new XSSFWorkbook(fis);
FileOutputStream fos = new FileOutputStream("C:\\Users\\yan\\Desktop\\新时代学生字典专项(14).xlsx")) {
Sheet sheet = workbook.getSheetAt(0);
String lastValueCol1 = null;
String lastValueCol2 = null;
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) continue;
// Process the first column
Cell cell1 = row.getCell(0);
if (cell1 != null) {
String cellValue1 = cell1.getStringCellValue();
if (cellValue1.equals(lastValueCol1)) {
cell1.setCellValue(""); // Clear cell content
} else {
lastValueCol1 = cellValue1;
}
}
// Process the second column
Cell cell2 = row.getCell(1);
if (cell2 != null) {
String cellValue2 = cell2.getStringCellValue();
if (cellValue2.equals(lastValueCol2)) {
lastValueCol2 = cellValue2;
} else {
lastValueCol2 = cellValue2;
}
}
}
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
}
}
}
将一行分割到新的表格中按符号排列
class CopyExcelProcessor {
public static void main(String[] args) throws IOException {
// 读取 Excel 文件
FileInputStream fis = new FileInputStream("C:\\Users\\yan\\Desktop\\新时代学生字典专项(1).xlsx");
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
// 创建新的 Excel 文件
Workbook newWorkbook = new XSSFWorkbook();
Sheet newSheet = newWorkbook.createSheet();
int newRowNum = 0;
for (Row row : sheet) {
Cell cell0 = row.getCell(0);
Cell cell1 = row.getCell(1);
Cell cell = row.getCell(2); // 读取第3列
if (cell != null && cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
String cellValue0 = cell0 != null ? cell0.getStringCellValue() : "";
String cellValue1 = cell1 != null ? cell1.getStringCellValue() : "";
String[] parts = splitByBrackets(cellValue);
for (int i = 0; i < parts.length; i++) {
if (i % 2 == 1) {
Row newRow = newSheet.createRow(newRowNum++);
Cell newCell0 = newRow.createCell(0);
Cell newCell1 = newRow.createCell(1);
Cell newCell = newRow.createCell(2);
newCell0.setCellValue(cellValue0);
newCell1.setCellValue(cellValue1);
newCell.setCellValue(parts[i].replace("【", "").replace("【", ""));
}
}
}
}
// 写入到新 Excel 文件
FileOutputStream fos = new FileOutputStream("C:\\Users\\yan\\Desktop\\新时代学生字典专项(13).xlsx");
newWorkbook.write(fos);
fos.close();
// 关闭工作簿和输入流
workbook.close();
fis.close();
newWorkbook.close();
}
private static String[] splitByBrackets(String input) {
return input.split("(?<=【)|(?=【)");
}
}
移除(“〖.*?〗”) 文本
class backExcelProcessor {
public static void main(String[] args) {
String inputFilePath = "C:\\Users\\yan\\Desktop\\待删除的.xlsx";
String outputFilePath = "C:\\Users\\yan\\Desktop\\待删除的output.xlsx";
try (FileInputStream fis = new FileInputStream(new File(inputFilePath));
XSSFWorkbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
Pattern pattern = Pattern.compile("〖.*?〗");
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == CellType.STRING) {
String cellValue = cell.getStringCellValue();
Matcher matcher = pattern.matcher(cellValue);
String newValue = matcher.replaceAll(""); // 替换匹配的内容
cell.setCellValue(newValue);
}
}
}
// 写入到新文件
try (FileOutputStream fos = new FileOutputStream(new File(outputFilePath))) {
workbook.write(fos);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}