java - 如何从字符串中获取 ipAddresses 列表?
问题描述
我有一个包含多个 IP 地址的大字符串。
String str1 = "10.0.0.2 - frank [10/Oct/2000:13:55:36" + " -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326 "
+ "\"104.244.253.29\" \"Mozilla/4.08 " + "[en] (Win98; I ;104.30.244.2)\"";
我使用将字符串放入字符串数组split
,然后检查单词中包含 3.
的每个单词。但 dint 被发现是一个万无一失的解决方案。
我想使用正则表达式并获得List<String>
ipAddresses 返回
public List<String> findIPs(String str) {
//implement it via regex
}
在这里,我想要方法将返回的字符串列表中的 3 项。
我怎样才能通过正则表达式来做到这一点?
提前致谢。
解决方案
String str1 = "10.0.0.2 - frank [10/Oct/2000:13:55:36" + " -0700] \"GET /apache_pb.gif HTTP/1.0\" 200 2326 "
+ "\"104.244.253.29\" \"Mozilla/4.08 " + "[en] (Win98; I ;104.30.244.2)\"";
Pattern p = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
Matcher m = p.matcher(str1);
while (m.find()){
System.out.println(m.group(0));
}
推荐阅读
- python - python3 tkinter和matplot冲突
- angular - Ionic 动态显示选项卡图标 Angular
- c++ - 如何使用 GTest 测试命令行选项解析器
- linux - Linux Huge pages 内存使用计算
- bash - bash - executing ssh command in a loop
- sql - 通过按最新记录排序来应用组
- c++ - 为什么升序排序 C++ 不能正常工作?
- python - Django - 媒体文件在生产中进行 404(Azure 存储)
- css - 为什么我的 CSS 变量在尝试在动画中使用时仍未使用?
- perl - 我如何获得会话