導航:首頁 > 編程語言 > javaip地址正則表達式

javaip地址正則表達式

發布時間:2025-09-25 21:24:20

① js正則表達式 和java正則表達式一樣嗎

javascript中的正則表達式和java的正則表達式基本上是相同的,區別在於分組引用和對象,方法
具體區別:
1).javascript正則表達式創建有兩種方法:
a.顯式創建:
var re = new RegExp("正則表達式模式串");
re.test(要校驗或處理的源字元串);

b.隱式創建:
var re = /正則表達式模式串/;
要校驗或處理的源字元串.match(re);

2).分組捕獲對象引用方式不同
javascript也是使用"()"進行分組,但是捕獲對象用RegExp對象的$1到$99來引用捕獲對象。

附錄:常用的javascript正則表達式,java的也類似
ip地址校驗正則表達式(IPv4):
/^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])(/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])){3}$/

Email校驗正則表達式:
/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(/.[a-zA-Z0-9_-]+)+$/

格式為:2010-10-08類型的日期格式校驗正則表達式:
/^/d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]/d|3[0-1])$/

格式為:23:11:34類型的時間格式校驗正則表達式:
/^([0-1]/d|2[0-3]):[0-5]/d:[0-5]/d$/

樓主結貼吧,查資料的時候看到的

② java中如何獲取到本機的外網ip地址

java獲取本機的外網ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 獲取本機外網IP地址
* 思想是訪問網站http://checkip.dyndns.org/,得到返回的文本後解析出本機在外網的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外網IP提供者的網址
private String externalIpProviderUrl;

// 本機外網IP地址
private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);
}

/**
* 從外網提供者處獲得包含本機外網地址的字元串
* 從http://checkip.dyndns.org返回的字元串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {
// 輸入流
InputStream in = null;

// 到外網提供者的Http連接
HttpURLConnection httpConn = null;

try {
// 打開連接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection) url.openConnection();

// 連接設置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// 獲取連接的輸入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根據實際情況調整

// 讀取到數組中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// 將位元組轉化為為UTF-8的字元串
String receivedString=new String(bytes,"UTF-8");

// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 出現異常則返回空
return null;
}

/**
* 使用正則表達式解析返回的HTML文本,得到本機外網地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}

/**
* 得到本機外網地址,得不到則為空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}

public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");

System.out.println(fetcher.getMyExternalIpAddress());
}
}

③ JAVA正則表達式

http://blog.pfan.cn/iamben250/34352.html這是我的blog上面的詳細介紹。配中文字元的正則表達式: [\u4e00-\u9fa5]

匹配雙位元組字元(包括漢字在內):[^\x00-\xff]

應用:計算字元串的長度(一個雙位元組字元長度計2,ASCII字元計1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正則表達式:\n[\s| ]*\r

匹配HTML標記的正則表達式:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的正則表達式:(^\s*)|(\s*$)

應用:javascript中沒有像vbscript那樣的trim函數,我們就可以利用這個表達式來實現,如下:

String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

利用正則表達式分解和轉換IP地址:

下面是利用正則表達式匹配IP地址,並將IP地址轉換成對應數值的Javascript程序:

function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正則表達式
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

不過上面的程序如果不用正則表達式,而直接用split函數來分解可能更簡單,程序如下:

var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

匹配Email地址的正則表達式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配網址URL的正則表達式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

利用正則表達式去除字串中重復的字元的演算法程序:[註:此程序不正確,原因見本貼回復]

var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //結果為:abcefgi

我原來在CSDN上發貼尋求一個表達式來實現去除重復字元的方法,最終沒有找到,這是我能想到的最簡單的實現方法。思路是使用後向引用取出包括重復的字元,再以重復的字元建立第二個表達式,取到不重復的字元,兩者串連。這個方法對於字元順序有要求的字元串可能不適用。

得用正則表達式從URL地址中提取文件名的javascript程序,如下結果為page1

s=" http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)

利用正則表達式限制網頁表單里的文本框輸入內容:

用正則表達式限制只能輸入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正則表達式限制只能輸入全形字元: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正則表達式限制只能輸入數字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正則表達式限制只能輸入數字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 出處:藍色理想
前一段時間寫了2段EmEditor的宏,用來統計代碼行數和簡單的規約檢查,稍微整理一下,
下面是從EmEditor的Q&A的提取的實例:雙引號包含的字元串
strings surrounded by double-quotation marks
「.*?」 [ ]包含的字元串
strings surrounded by [ ]
\[[^\[]*?\] 變數名
variable names
[a-zA-Z_][a-zA-Z_0-9]* IP 地址
IP addresses
([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}) 網頁地址
URL
(\S+)://([^:/]+)(:(\d+))?(/[^#\s]*)(#(\S+))? 各行Tab以後的文字列
lines followed by a tab
\t.*$ 平仮名 ひらがな
Hiragana
[\x{3041}-\x{309e}] 全形片仮名 全形カタカナ
Full-width Katakana
[\x{309b}-\x{309c}\x{30a1}-\x{30fe}] 半形仮名 半形カナ
Half-width Kana
[\x{ff61}-\x{ff9f}] 中日韓 漢字
CJK ideographs
[\x{3400}-\x{9fff}\x{f900}-\x{fa2d}] 中日韓 漢字元號
CJK ideograph marks
[\x{3000}-\x{3037}] 韓國字元
Hangul
[\x{1100}-\x{11f9}\x{3131}-\x{318e}\x{ac00}-\x{d7a3}] 行頭插入 //
Insert // at start of lines
Find: ^
Replace with: // 刪除行頭 //
Remove // at end of lines
Find: ^//
Replace: 刪除行後的空白文字(包含空格和製表位 Space+Tab)
Remove trailing whitespaces
Find: \s+?$
Replace with: 將(abc)替換為[abc]
Replace (abc) with [abc]
Find: \((.*?)\)
Replace: \[\1\] 將<H3 …>替換為<H4 …>
Replace <H3 …> with <H4 …>
Find: <H3(.*?)>
Replace: <H4\1> 將9/13/2003替換為2003年9月13日
Replace 9/13/2003 with 2003.9.13
Find: ([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})
Replace: \3年\1月\2日 將字母a-z替換為大寫字母
Uppercase characters from a to z
Find: [a-z]
Replace: \U\0 首字母大寫
Capitalize all words
Find: ([a-zA-Z])([a-zA-Z]*)
Replace: \U\1\L\2

④ java的正則表達式有什麼作用

所有編程語言均支持正則表達。主要方便檢索數據看情況決定是否使用正則。

閱讀全文

與javaip地址正則表達式相關的資料

熱點內容
c語言遞歸時編譯失敗 瀏覽:976
單片機存儲格式 瀏覽:612
反編譯需要語言學習嗎 瀏覽:347
移動為什麼無法連接伺服器 瀏覽:166
便宜阿里雲伺服器購買 瀏覽:924
存50億以上數據需要什麼伺服器 瀏覽:321
單片機第四版課後答案 瀏覽:279
javaip地址正則表達式 瀏覽:165
編譯錯誤意思 瀏覽:698
c51單片機馬達原理 瀏覽:54
三菱plcfx1s編程手冊 瀏覽:100
優摩手環app叫什麼 瀏覽:510
抖音1314520的演算法介紹 瀏覽:467
php刪除所有空格 瀏覽:89
ug編程有幾種加工模式 瀏覽:432
如何隱藏安卓機密 瀏覽:377
linuxsocket描述符 瀏覽:401
程序員學習做飯 瀏覽:145
c語言系統編程pdf 瀏覽:271
新建文件夾是放在哪裡的 瀏覽:828