導航:首頁 > 編程語言 > java網址正則

java網址正則

發布時間:2022-07-23 05:52:27

java中在網頁源代碼中匹配這個網址的正則表達式是什麼

packagetest;

importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

publicclassTest
{
publicstaticvoidmain(String[]args)
{
Stringinput="<ahref="forum.php?mod=viewthread&tid=214172&extra=page%3D1"onclick="atarget(this)"class="sxst">博士視頻講師,1小時180+</a><ahref="forum.php?mod=viewthread&tid=215520&extra=page%3D1"onclick="atarget(this)"class="sxst">周日2月1日麥頌KTV唱歌交友活動通知</a>";
Stringregex="(?i)href[\="'\s]+([^"']+)["']?";
Patternpattern=Pattern.compile(regex);
Matchermatcher=pattern.matcher(input);
while(matcher.find())
{
System.out.println(matcher.group(1));
}
}
}

Ⅱ url在java中如何用正則匹配

這需要用到正則嗎?直接url.contains("bbs") 不就行了:

publicclassTest{
publicstaticvoidmain(String[]args){
Stringurl="http://www.xxx.aaa.cn/bbs/topic/xxx";
System.out.println(url.contains("bbs"));
}
}


一定要正則的話可以這樣:

importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassCopyOfTest{
publicstaticvoidmain(String[]args){
String[]urls={
"http://www.xxx.aaa.cn/bbs/topic/xxx",
"http://www.xxx.aaa.com/bbs/topic/xxx",
"http://www.yyy.bbb.cn/aas/xxx/xxx/x"
};
Stringregex="/bbs";

Patternp=Pattern.compile(regex);
for(Stringurl:urls){
Matcherm=p.matcher(url);
if(m.find()){
System.out.println(url);
}
}
}
}

Ⅲ Java網頁正則這個咋寫

前面的區號還有4位的,不然 0349 之類 \d{3} 的話這類就過不了了。

Ⅳ Java中什麼是正則表達式

正則表達式 就是將一個類型的數據 按照一定方式 寫成正則表達式的形式 這樣可以辨別數據是否符合這一類型的數據 比如我們的身份證號碼 就是有一定的規則的 這樣我們寫一個 正則表達式 和別人輸入的身份證號碼 比對 如果符合這個正則表達式的規則 就是身份證號碼

Ⅳ java 正則表達式是什麼

不同情況下的正則表達式:

Ⅵ java正則表達式提取網址

  1. 用字元串的split方法

    var ip = '127.111.1.112:8080';

    var addr = ip.split(':')[0];

    var port = ip.split(':')[1];

  2. 用正則

    var reg=/(d{1,3}.d{1,3}.d{1,3}.d{1,3}):(d{1,4})/;

    var ip = '127.111.1.112:8080';

    var addr = ip.replace(reg,'$1');

    var port = ip.replace(reg,'$2');

  3. 還可以間接使用字元串其他的方法,或者是數組的

Ⅶ java中在網頁源代碼中匹配網址的正則表達式是什麼

簡單點的可以是:

(?is)hrefs*=s*["']((?!javascript:)[^"']+)["']

提取第2個捕獲組結果就是 你想要的連接。

Ⅷ 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的正則表達式匹配這樣的網址

Java中正則表達式匹配的語法規則:以下是整理出來的Java下運用正則表達式實現匹配的程序案例,代碼如下:package org.luosijin.test;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 正則表達式 * @version V5.0 * @author Admin * @date 2015-7-25 */public class Regex { /** * @param args * @author Admin * @date 2015-7-25 */ public static void main(String[] args) { Pattern pattern = Pattern.compile("b*g"); Matcher matcher = pattern.matcher("bbg"); System.out.println(matcher.matches()); System.out.println(pattern.matches("b*g","bbg")); //驗證郵政編碼 System.out.println(pattern.matches("[0-9]{6}", "200038")); System.out.println(pattern.matches("//d{6}", "200038")); //驗證電話號碼 System.out.println(pattern.matches("[0-9]{3,4}//-?[0-9]+", "02178989799")); getDate("Nov 10,2009"); charReplace(); //驗證身份證:判斷一個字元串是不是身份證號碼,即是否是15或18位數字。 System.out.println(pattern.matches("^//d{15}|//d{18}$", "123456789009876")); getString("D:/dir1/test.txt"); getChinese("welcome to china,江西奉新,welcome,你!"); validateEmail("[email protected]"); } /** * 日期提取:提取出月份來 * @param str * @author Admin * @date 2015-7-25 */ public static void getDate(String str){ String regEx="([a-zA-Z]+)|//s+[0-9]{1,2},//s*[0-9]{4}"; Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(str); if(!matcher.find()){ System.out.println("日期格式錯誤!"); return; } System.out.println(matcher.group(1)); //分組的索引值是從1開始的,所以取第一個分組的方法是m.group(1)而不是m.group(0)。 } /** * 字元替換:本實例為將一個字元串中所有包含一個或多個連續的「a」的地方都替換成「A」。 * * @author Admin * @date 2015-7-25 */ public static void charReplace(){ String regex = "a+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher("okaaaa LetmeAseeaaa aa booa"); String s = matcher.replaceAll("A"); System.out.println(s); } /** * 字元串提取 * @param str * @author Admin * @date 2015-7-25 */ public static void getString(String str){ String regex = ".+/(.+)$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); if(!matcher.find()){ System.out.println("文件路徑格式不正確!"); return; } System.out.println(matcher.group(1)); } /** * 中文提取 * @param str * @author Admin * @date 2015-7-25 */ public static void getChinese(String str){ String regex = "[//u4E00-//u9FFF]+";//[//u4E00-//u9FFF]為漢字 Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); while(matcher.find()){ sb.append(matcher.group()); } System.out.println(sb); } /** * 驗證Email * @param email * @author Admin * @date 2015-7-25 */ public static void validateEmail(String email){ String regex = "[0-9a-zA-Z]+@[0-9a-zA-Z]+//.[0-9a-zA-Z]+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); if(matcher.matches()){ System.out.println("這是合法的Email"); }else{ System.out.println("這是非法的Email"); } }}

閱讀全文

與java網址正則相關的資料

熱點內容
java自動格式化 瀏覽:617
ipad怎麼查看文件夾大小 瀏覽:581
手工粘土解壓球 瀏覽:550
在線視頻教育源碼 瀏覽:39
快四十學什麼編程 瀏覽:754
gnumakelinux 瀏覽:537
視易峰雲伺服器怎麼改系統 瀏覽:535
javamap取值 瀏覽:768
mac和win磁碟加密軟體 瀏覽:474
蘋果為什麼會連接不到伺服器 瀏覽:726
pdf格式文件如何保存 瀏覽:303
小霸王伺服器tx什麼意思 瀏覽:75
解釋dns命令 瀏覽:584
dmx512怎麼編程 瀏覽:744
北京雲主機17t雲伺服器 瀏覽:232
php伺服器url地址 瀏覽:440
哪裡看書免費app 瀏覽:437
php刪除數組中重復值 瀏覽:786
經理下命令咱都別說話是什麼意思 瀏覽:625
上海風機可編程式控制制器價格 瀏覽:249