gpt4 book ai didi

java - 如何在java中提取特定的字符串??

转载 作者:行者123 更新时间:2023-11-29 03:22:37 24 4
gpt4 key购买 nike

<分区>

我的文本文件包含数据,我应该只从中提取 ISDN 字符串。 ISDN 不断重复,它的值(value)也在不断变化。我得到的输出只有 2 个方括号(“[]”),输入文件中没有方括号。

这里是输入文本文件

CDR MOC={
RecordType=0(MOC)
sequenceNumber=5346435
callingIMSI=40589345341354118911
callingIMEI=80FC64634440F
callingNumber{
AddInd=H'134
NumPlan=H'245
ISDN=45645734
}

这是我的代码

public class MyFile {

private static final Pattern ISDN = Pattern.compile("ISDN=(.*)");

public List<String> getISDNsFromFile(final String fileName)
throws IOException {
final Path path = Paths.get(fileName);
final List<String> ret = new ArrayList<>();

Matcher m;
String line;

try (
final BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);) {
while ((line = reader.readLine()) != null) {
m = ISDN.matcher(line);
if (m.matches()) {
ret.add(m.group(1));
}
}
}

return ret;
}

public void writeTextFile(String filename, List<String> s) {
FileWriter output = null;
try {
output = new FileWriter(filename);
try (BufferedWriter writer = new BufferedWriter(output)) {
String ss = String.valueOf(s);
writer.append(ss);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (output != null) {
try {
output.flush();
output.close();
} catch (IOException e) {
}
}
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com