如何检查文本是否包含IP地址?

编程入门 行业动态 更新时间:2024-10-12 03:17:27
本文介绍了如何检查文本是否包含IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经四处寻找,但没有发现任何可行的东西。如何检查文本是否包含IP地址?我试过这个但它不起作用:

I've searched around but have not found anything that works. How do I check if a text contains an IP address? I have tried this but it does not work:

public boolean ip(String a_text) { String ip_filter = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; if (a_text.toLowerCase().contains(ip_filter.toLowerCase())){ return true; } return false; }

推荐答案

您正在尝试使用使用的正则表达式包含方法。并且该方法不接收正则表达式作为参数。它接收一个普通的字符串。您应该尝试使用 Pattern 和匹配器。

You're trying to use a Regular Expression with the contains method. And that method does not receive a regex as argument. It receives a plain String. You should try using Pattern and Matcher.

这里是例如:

public static boolean ip(String text) { Pattern p = Patternpile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); Matcher m = p.matcher(text); return m.find(); }

编辑:我将模式更新为更多合适的,这里。

EDIT: I updated the pattern to a more suitable one, found here.

更多推荐

如何检查文本是否包含IP地址?

本文发布于:2023-11-10 14:38:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   地址   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!