java 正則基礎(chǔ)入門教程_第1頁
java 正則基礎(chǔ)入門教程_第2頁
java 正則基礎(chǔ)入門教程_第3頁
java 正則基礎(chǔ)入門教程_第4頁
java 正則基礎(chǔ)入門教程_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

java正則(java正則)

-forexample,inastringcontainingverification

//searchstartswithJava,endswitharbitrarystring

Patternpattern=Pattern,compile(""Java.*");

Matchermatcher=pattern,matcher(,,Javaisnothumanz,);

Booleanb=matcher,matches();

//whentheconditionsaresatisfied,willreturntrue,

otherwiseitreturnsfalse

System,out.printin(B);

Theconditionstostringsegmentation

Patternpattern=Pattern,compile("[∣]+.");

String[]STRs=pattern,split(,,JavaHelloWorldJava,Hello,

WorldISun,/);

For(inti=0;i<strs.length;i++){

System,out.printin(strs[i]);

Thereplacementtext(firstcharacter)

Patternpattern=Pattern,compile(regularexpression);

Matchermatcher=pattern,matcher(,,regularexpressionHello

World,regularexpressionHelIoWorldz,);

Replace//firstinlinewithregulardata

System,out.printin(matcher.replaceFirst("Java"));

Thereplacementtext(all)

Patternpattern=Pattern,compile(regularexpression);

Matchermatcher=pattern,matcher("regularexpressionHello

World,regularexpressionHelIoWorldz,);

Replace//firstinlinewithregulardata

System,out.printin(matcher.replaceAll("Java"));

Thetextreplacement(replacementcharacter)

Patternpattern=Pattern,compile(regularexpression);

Matchermatcher=pattern,matcher("regularexpressionHello

World,regularexpressionHelIoWOrICr);

StringBufferSBR=newStringBuffer();

While(matcher,find()){

Matcher.appendReplacement(SBR,"Java");

Matcher.appendTail(SBR);

System,out.printin(sbr.toString());

Thee-mailaddressisverified

Stringstr="ceponline@"”;

Patternpattern=Pattern,compile("[??w??.\\-]+@

([??w??-]+??.)+[??w??-]+”,Pattern.CASE_INSENSITIVE);

Matchermatcher=pattern,matcher(STR);

System,out.printin(matcher,matches());

TheremovalofHTMLmarkers

Patternpattern=Pattern,compile("<.+?>”,Pattern.DOTALL);

Matchermatcher=pattern,matcher(,,<ahref=?"index,html?”>

homepage<∕a>");

Stringstring=matcher.replaceAll;

System,out.printin(string);

ThecorrespondingconditionstringlookupinHTML

Patternpattern=Pattern,compile("href=?"(1?);

Matchermatcher=pattern,matcher(,,<ahref=?"index,html?”>

homepage<∕a>");

If(matcher,find())

System,out.printin(matcher,group(1));

)

Theinterceptionofhttp://address

//urlinterception

Patternpattern=Pattern,compile("{1}[??w??.??一/

(http://|https://):]+");

Matchermatcher=pattern,matcher

(,,dsdsds<http://dsds//gfgffdfd>fdfz/);

StringBufferbuffer=newStringBuffer();

While(matcher,find()){

Buffer,append(matcher,group());

Buffer,append(“?r?n");

System,out.printin(buffer.toString());

Wereplacethespecifiedtextintext

StringSTR="thecurrentdevelopmenthistoryofJavaisfrom

{0}-⑴〃;

字符串[□]{新的字符串字0",字符5"},新的字符串□“{1”,

“2007”};

系統(tǒng)。出來。printin(取代(Str對象));

字符串替換(串SoUrCeString,對象[]對象){

字符串=sourcestring;

對于(int=i0;i〈對象.長度;i++)

字符串(□)對象(i);

模式模式=編譯(結(jié)果[0]);

匹配匹配=模式匹配(溫度);

溫度=匹配。替換(結(jié)果[1]);

回復(fù);

?以正則條件查詢指定目錄下文件

//用于緩存文件列表

私人文件=新arraylist()ArrayList;

//用于承載文件路徑

_path字符串;

//用于承載未合并的正則公式

JegeXP字符串;

類myfilefilter實現(xiàn)過濾{

/**

*匹配文件名稱

*/

公共布爾接受(文件文件){

嘗試{

模式=模式編譯(_regexp);

匹配匹配=模式匹配(文件。getname());

matches()返回匹配;

}catch(異常E){

返回true;

}

/**

*解析輸入流

*@Param輸入

*/

filesanalyze(字符串路徑字符串的正則表達式){

作用:取文件名(路徑,正則表達式);

/**

*分析文件名并加入文件

*@param輸入

*/

PriVateVOid作用:取文件名(字符串路徑字符串的正則表達式){

//目錄

_path=路徑;

_regexp=表達式;

文件目錄=新的文件(_path);

文件[]filesfile=目錄。listfiles(新Hiyfilefilter());

如果(filesfile==null)返回;

為(j=0;J<filesfile,length;j++){

文件。添加(filesfile[J]);

}

返回;

*顯示輸出信息

*?param出來

*/

公共無效打?。ㄝ敵隽鳎﹞

迭代器元素=文件。();

而(元素。hasnext()){

XmI文件(文件)的元素。next();

出來。printin(文件。getpath());

}

publicstaticVOid輸出(字符串路徑字符串的正則表達式){

fiIesanaIyZefilegroUPl=新fiIesanaIyZe(路徑,正則表達式);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論