Java 正则表达式提取字符串:rule 0 deny ip destination 30.18.74.0 0.0.0.255
可以使用正则表达式来提取字符串中的特定部分。以下是一个示例代码,可以提取出字符串中的 0、30.18.74.0 和 0.0.0.255。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = 'rule 0 deny ip destination 30.18.74.0 0.0.0.255';
String ruleId = '';
String ipDestination = '';
String ipMask = '';
// 正则表达式匹配
String regex = 'rule (\d+) deny ip destination (\d+\.\d+\.\d+\.\d+) (\d+\.\d+\.\d+\.\d+)';
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// 提取匹配结果
if (matcher.find()) {
ruleId = matcher.group(1);
ipDestination = matcher.group(2);
ipMask = matcher.group(3);
}
// 输出结果
System.out.println('ruleId: ' + ruleId);
System.out.println('ipDestination: ' + ipDestination);
System.out.println('ipMask: ' + ipMask);
}
}
输出结果为:
ruleId: 0
ipDestination: 30.18.74.0
ipMask: 0.0.0.255
原文地址: https://www.cveoy.top/t/topic/b3gk 著作权归作者所有。请勿转载和采集!