gpt4 book ai didi

Java正则表达式问题

转载 作者:行者123 更新时间:2023-12-01 16:39:29 25 4
gpt4 key购买 nike

我有一个大的 html 文件作为输入文本,我必须使用它来提取一些信息模式匹配。“区域”在某种程度上如下:

 some html text
<div debugState" style="display: none;">
Model: ModelCode[BR324]
Features: [S08TL, S0230, S0851, S0428, S01CD, S0879, S01CA, S08SP, S0698, S01CB, S0548, S08SC, S08TM, S01CC, S0801, S0258, P0668, S04AK]
Packages: [S0801]
</div>
some html text

我写了下面的代码。 (位于debInfo)是要扫描的html源。由于

Pattern model = Pattern.compile(".*(Model: ModelCode\\[\\w\\]).*, Pattern.DOTALL");
Pattern features = Pattern.compile(".*(Features: \\[\\w*\\]).*, Pattern.DOTALL");
Pattern packages = Pattern.compile(".*(Packages: \\[\\w*\\]).*, Pattern.DOTALL");


Matcher m1 = model.matcher(debInfo);
Matcher m2 = features.matcher(debInfo);
Matcher m3 = packages.matcher(debInfo);

boolean a = m1.matches();
boolean b = m2.matches();
boolean c = m3.matches();

System.out.println("matches(); " + a + " " + b + " " + c + " " + "\n" + debInfo);

我没有得到匹配的:-(。我究竟做错了什么?提前致谢(非常感谢!)

最佳答案

您在(正确转义的)方括号内使用 \\w 。只匹配一个字符。请尝试使用 \\w+\\w*

此外,您在字符串文字中包含了 , Pattern.DOTALL,我认为这是一个拼写错误:

Pattern model = Pattern.compile(".*(Model: ModelCode\\[\\w+\\]).*", Pattern.DOTALL);

另请注意,对于以逗号和空格分隔的 Features 列表 \\w* 不起作用,您需要类似 [\\w\\s,]*.

关于Java正则表达式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380988/

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