gpt4 book ai didi

java - 正则表达式模式未获取匹配项

转载 作者:行者123 更新时间:2023-12-01 09:40:15 25 4
gpt4 key购买 nike

当我在线使用正则表达式检查器(例如Regular Expression Tester)时,我正在使用的一些字符串模式不会在运行时为我获取匹配项。 ,我发现我的模式为我获取了所需的输出。

public class RegexMatches
{
public static void main( String args[] ){
String input = "[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms";

try{
String op[] = parseIndividualIP(input);
System.out.println(op[1]);
System.out.println(op[8]);
}
catch (Exception e){
e.printStackTrace();
}
}

// Doesn't Work...
public static String[] parseIndividualIP(String input) throws IPAddressNotFoundException {
// Capture the ip-address after 'x byes from'
Pattern p = Pattern.compile("bytes\\s+from\\s+([\\d,\\.]+):");
Matcher m = p.matcher(input);
if (m.find()){
int i = 0;
String[] s = new String[10];
while(m.find()){
s[i] = m.group(++i);
}
return s;
}
else
throw new IPAddressNotFoundException();
}

}

我不知道为什么在运行时我没有匹配项,也不知道应该如何调试这个问题。由于该模式在运行前后进行了交叉检查。

输入字符串 -

[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms

使用的正则表达式模式 -

bytes\\s+from\\s+([\\d,\\.]+):

最佳答案

主要问题是使用m.group(++i)。在正则表达式中,您有一个捕获组来捕获 IP 地址 (([\\d,\\.]+)),这意味着您应该调用 m.group(1 ) 因为它返回第一组捕获的字符串。

Pattern 对象是线程安全的,因此可以编译一次并重用同一实例。

以下代码包含组修复以及一些可读性修改。将数组的用法更改为链表,如果没有找到匹配项,该方法将返回空列表,而不是抛出异常。

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
private static final Pattern PATTERN = Pattern.compile("bytes\\s+from\\s+([\\d,\\.]+):");

public static void main(String args[]) {
String input = "[1463895254]PING www.andi.dz (213.179.181.44) 100(128) bytes of data.[1463895254]108 bytes from 213.179.181.44: icmp_seq=1 ttl=54 time=195 ms[1463895255]108 bytes from 213.179.181.44: icmp_seq=2 ttl=54 time=202 ms[1463895256]108 bytes from 213.179.181.44: icmp_seq=3 ttl=54 time=180 ms[1463895257]108 bytes from 213.179.181.44: icmp_seq=4 ttl=54 time=200 ms[1463895258]108 bytes from 213.179.181.44: icmp_seq=5 ttl=54 time=206 ms[1463895259]108 bytes from 213.179.181.44: icmp_seq=6 ttl=54 time=188 ms[1463895260]108 bytes from 213.179.181.44: icmp_seq=7 ttl=54 time=182 ms[1463895261]108 bytes from 213.179.181.44: icmp_seq=8 ttl=54 time=223 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=9 ttl=54 time=187 ms[1463895263]108 bytes from 213.179.181.44: icmp_seq=10 ttl=54 time=199 ms";

List<String> op = parseIndividualIP(input);
op.forEach(System.out::println);
}

public static List<String> parseIndividualIP(String input) {
Matcher m = PATTERN.matcher(input);
List<String> ips = new LinkedList<>();
while (m.find()) {
ips.add(m.group(1));
}
return ips;
}
}

关于java - 正则表达式模式未获取匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511014/

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