本文介绍了如何检查文本是否包含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地址?
发布评论