Java用正則表示式判斷是否為IP
- 程式碼
/**
1. @Title:IpAddress.java
2. @Package:com.you.dao
3. @Description:用正則表示式判斷是否為IP
5. @date: 2014年3月4日 下午10:55:06
6. @Version V1.2.3
*/
package com.you.dao;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
7. @類名:IpAddress
8. @描述:用正則表示式判斷是否為IP
9. @Author :Administrator
10. @date: 2014年3月4日 下午10:55:06
*/
public class IpAddress
{
public static class IpAdd
{
public boolean isIP(String addr)
{
if(addr.length() < 7 || addr.length() > 15 || "".equals(addr))
{
return false ;
}
/**
* 判斷IP格式和範圍
*/
String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(addr);
boolean ipAddress = mat.find();
return ipAddress;
}
}
/**
* @Title : main
* @Type : IpAddress
* @date : 2014年3月4日 下午10:55:06
* @Description : IP可能的範圍是0-255.0-255.0-255.0-255
* @param args
*/
public static void main(String[] args)
{
/**
* 符合IP地址的範圍
*/
String oneAddress = "10.127.30.45";
/**
* 符合IP地址的長度範圍但是不符合格式
*/
String twoAddress = "127.30.45";
/**
* 不符合IP地址的長度範圍
*/
String threeAddress = "7.0.4";
/**
* 不符合IP地址的長度範圍但是不符合IP取值範圍
*/
String fourAddress = "255.255.255.2567";
IpAdd ipAdd = new IpAdd();
//判斷oneAddress是否是IP
System.out.println(ipAdd.isIP(oneAddress));
//判斷twoAddress是否是IP
System.out.println(ipAdd.isIP(twoAddress));
//判斷threeAddress是否是IP
System.out.println(ipAdd.isIP(threeAddress));
//判斷fourAddress是否是IP
System.out.println(ipAdd.isIP(fourAddress));
}
}
- 執行結果
true
false
false
false
附錄
- find()方法是部分匹配,是查詢輸入串中與模式匹配的子串,如果該匹配的串有組還可以使用group()函式。
matches()是全部匹配,是將整個輸入串與模式匹配,如果要驗證一個輸入的資料是否為數字型別或其他型別,一般要用matches()。
Pattern pattern= Pattern.compile(“.?,(.)”);Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
return matcher.group(1);
}- 詳解:
matches
public static boolean matches(String regex, CharSequence input)
編譯給定正則表示式並嘗試將給定輸入與其匹配。
呼叫此便捷方法的形式
Pattern.matches(regex, input);
Pattern.compile(regex).matcher(input).matches() ;
如果要多次使用一種模式,編譯一次後重用此模式比每次都呼叫此方法效率更高。
引數:
regex - 要編譯的表示式
input - 要匹配的字元序列
丟擲:
PatternSyntaxException - 如果表示式的語法無效
find
public boolean find()嘗試查詢與該模式匹配的輸入序列的下一個子序列。
此方法從匹配器區域的開頭開始,如果該方法的前一次呼叫成功了並且從那時開始匹配器沒有被重置,則從以前匹配操作沒有匹配的第一個字元開始。
如果匹配成功,則可以通過 start、end 和 group 方法獲取更多資訊。
matcher.start() 返回匹配到的子字串在字串中的索引位置.
matcher.end()返回匹配到的子字串的最後一個字元在字串中的索引位置.
matcher.group()返回匹配到的子字串
返回:
當且僅當輸入序列的子序列匹配此匹配器的模式時才返回 true。
4.部分JAVA正則表示式例項
①字元匹配
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(str); // 操作的字串
boolean b = m.matches(); //返回是否匹配的結果
System.out.println(b);
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(str); // 操作的字串
boolean b = m. lookingAt (); //返回是否匹配的結果
System.out.println(b);
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(str); // 操作的字串
boolean b = m..find (); //返回是否匹配的結果
System.out.println(b);
②分割字串
Pattern pattern = Pattern.compile(expression); //正則表示式
String[] strs = pattern.split(str); //操作字串 得到返回的字串陣列
③替換字串
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(text); // 操作的字串
String s = m.replaceAll(str); //替換後的字串
④查詢替換指定字串
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(text); // 操作的字串
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find()) {
m.appendReplacement(sb, str);
i++; //字串出現次數
}
m.appendTail(sb);//從擷取點將後面的字串接上
String s = sb.toString();
⑤查詢輸出字串
Pattern p = Pattern.compile(expression); // 正則表示式
Matcher m = p.matcher(text); // 操作的字串
while (m.find()) {
matcher.start() ;
matcher.end();
matcher.group(1);
}