溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java正則表達式的實例用法

發(fā)布時間:2021-08-25 18:40:54 來源:億速云 閱讀:130 作者:chen 欄目:編程語言

本篇內容介紹了“java正則表達式的實例用法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

Java正則表達式實例詳解 
創(chuàng)建正則表達式
你可以從比較簡單的東西入手學習正則表達式。要想全面地掌握怎樣構建正則表達式,可以去看JDK文檔的java.util.regex的Pattern類的文檔。 字符  
B  字符B  
xhh  16進制值0xhh所表示的字符  
uhhhh  16進制值0xhhhh所表示的Unicode字符  
t  Tab  
n  換行符  
r  回車符  
f  換頁符  
e  Escape


正則表達式的強大體現(xiàn)在它能定義字符集(character class)。下面是一些最常見的字符集及其定義的方式,此外還有一些預定義的字符集: 字符集  
.  表示任意一個字符  
[abc]  表示字符a,b,c中的任意一個(與a|b|c相同)  
[^abc]  除a,b,c之外的任意一個字符(否定)  
[a-zA-Z]  從a到z或A到Z當中的任意一個字符(范圍)  
[abc[hij]]  a,b,c,h,i,j中的任意一個字符(與a|b|c|h|i|j相同)(并集)  
[a-z&&[hij]]  h,i,j中的一個(交集)  
s  空格字符(空格鍵, tab, 換行, 換頁, 回車)  
S  非空格字符([^s])  
d  一個數(shù)字,也就是[0-9]  
D  一個非數(shù)字的字符,也就是[^0-9]  
w  一個單詞字符(word character),即[a-zA-Z_0-9]  
W  一個非單詞的字符,[^w]


如果你用過其它語言的正則表達式,那么你一眼就能看出反斜杠的與眾不同。在其它語言里,""的意思是"我只是要在正則表達式里插入一個反斜杠。沒什么特別的意思。"但是在Java里,""的意思是"我要插入一個正則表達式的反斜杠,所以跟在它后面的那個字符的意思就變了。"舉例來說,如果你想表示一個或更多的"單詞字符",那么這個正則表達式就應該是"w+"。如果你要插入一個反斜杠,那就得用""。不過像換行,跳格之類的還是只用一根反斜杠:"nt"。

這里只給你講一個例子;你應該JDK文檔的java.util.regex.Pattern加到收藏夾里,這樣就能很容易地找到各種正則表達式的模式了。 邏輯運算符  
XY  X 后面跟著 Y  
X|Y  X或Y  
(X)  一個"要匹配的組(capturing group)". 以后可以用i來表示第i個被匹配的組。


邊界匹配符  
^  一行的開始  
$  一行的結尾  
b  一個單詞的邊界  
B  一個非單詞的邊界  
G  前一個匹配的結束


舉一個具體一些的例子。下面這些正則表達式都是合法的,而且都能匹配"Rudolph":

Rudolph
[rR]udolph
[rR][aeiou][a-z]ol.*
R.*數(shù)量表示符
"數(shù)量表示符(quantifier)"的作用是定義模式應該匹配多少個字符。

Greedy(貪婪的): 除非另有表示,否則數(shù)量表示符都是greedy的。Greedy的表達式會一直匹配下去,直到匹配不下去為止。(如果你發(fā)現(xiàn)表達式匹配的結果與預期的不符),很有可能是因為,你以為表達式會只匹配前面幾個字符,而實際上它是greedy的,因此會一直匹配下去。
Reluctant(勉強的): 用問號表示,它會匹配最少的字符。也稱為lazy, minimal matching, non-greedy, 或ungreedy。
Possessive(占有的): 目前只有Java支持(其它語言都不支持)。它更加先進,所以你可能還不太會用。用正則表達式匹配字符串的時候會產(chǎn)生很多中間狀態(tài),(一般的匹配引擎會保存這種中間狀態(tài),)這樣匹配失敗的時候就能原路返回了。占有型的表達式不保存這種中間狀態(tài),因此也就不會回頭重來了。它能防止正則表達式的失控,同時也能提高運行的效率。
Greedy  Reluctant  Possessive  匹配  
X?  X??  X?+  匹配一個或零個X  
X*  X*?  X*+  匹配零或多個X  
X+  X+?  X++  匹配一個或多個X  
X{n}  X{n}?  X{n}+  匹配正好n個X  
X{n,}  X{n,}?  X{n,}+  匹配至少n個X  
X{n,m}  X{n,m}?  X{n,m}+  匹配至少n個,至多m個X

再提醒一下,要想讓表達式照你的意思去運行,你應該用括號把'X'括起來。比方說:

abc+似乎這個表達式能匹配一個或若干個'abc',但是如果你真的用它去匹配'abcabcabc'的話,實際上只會找到三個字符。因為這個表達式的意思是'ab'后邊跟著一個或多個'c'。要想匹配一個或多個完整的'abc',你應該這樣:

(abc)+正則表達式能輕而易舉地把你給耍了;這是一種建立在Java之上的新語言。

CharSequence
JDK 1.4定義了一個新的接口,叫CharSequence。它提供了String和StringBuffer這兩個類的字符序列的抽象:

interface CharSequence {
 charAt(int i);
 length();
 subSequence(int start, int end);
 toString();
}為了實現(xiàn)這個新的CharSequence接口,String,StringBuffer以及CharBuffer都作了修改。很多正則表達式的操作都要拿CharSequence作參數(shù)。

Pattern和Matcher
先給一個例子。下面這段程序可以測試正則表達式是否匹配字符串。第一個參數(shù)是要匹配的字符串,后面是正則表達式。正則表達式可以有多個。在Unix/Linux環(huán)境下,命令行下的正則表達式還必須用引號。

當你創(chuàng)建正則表達式時,可以用這個程序來判斷它是不是會按照你的要求工作。 //: c12:TestRegularExpression.java
// Allows you to easly try out regular expressions.
// {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
import java.util.regex.*;
public class TestRegularExpression {
 public static void main(String[] args) {
   if(args.length < 2) {
     System.out.println("Usage:n" +
       "java TestRegularExpression " +
       "characterSequence regularExpression+");
     System.exit(0);
   }
   System.out.println("Input: "" + args[0] + """);
   for(int i = 1; i < args.length; i++) {
     System.out.println(
       "Regular expression: "" + args[i] + """);
     Pattern p = Pattern.compile(args[i]);
     Matcher m = p.matcher(args[0]);
     while(m.find()) {
       System.out.println("Match "" + m.group() +
         "" at positions " +
         m.start() + "-" + (m.end() - 1));
     }
   }
 }
} ///:~


Java的正則表達式是由java.util.regex的Pattern和Matcher類實現(xiàn)的。Pattern對象表示經(jīng)編譯的正則表達式。靜態(tài)的compile( )方法負責將表示正則表達式的字符串編譯成Pattern對象。正如上述例程所示的,只要給Pattern的matcher( )方法送一個字符串就能獲取一個Matcher對象。此外,Pattern還有一個能快速判斷能否在input里面找到regex的(注意,原文有誤,漏了方法名)

static boolean matches( regex,  input)以及能返回String數(shù)組的split( )方法,它能用regex把字符串分割開來。

只要給Pattern.matcher( )方法傳一個字符串就能獲得Matcher對象了。接下來就能用Matcher的方法來查詢匹配的結果了。

boolean matches()
boolean lookingAt()
boolean find()
boolean find(int start)matches( )的前提是Pattern匹配整個字符串,而lookingAt( )的意思是Pattern匹配字符串的開頭。

find( )
Matcher.find( )的功能是發(fā)現(xiàn)CharSequence里的,與pattern相匹配的多個字符序列。例如: //: c12:FindDemo.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class FindDemo {
 private static Test monitor = new Test();
 public static void main(String[] args) {
   Matcher m = Pattern.compile("w+")
     .matcher("Evening is full of the linnet's wings");
   while(m.find())
     System.out.println(m.group());
   int i = 0;
   while(m.find(i)) {
     System.out.print(m.group() + " ");
     i++;
   }
   monitor.expect(new String[] {
     "Evening",
     "is",
     "full",
     "of",
     "the",
     "linnet",
     "s",
     "wings",
     "Evening vening ening ning ing ng g is is s full " +
     "full ull ll l of of f the the he e linnet linnet " +
     "innet nnet net et t s s wings wings ings ngs gs s "
   });
 }
} ///:~


"w+"的意思是"一個或多個單詞字符",因此它會將字符串直接分解成單詞。find( )像一個迭代器,從頭到尾掃描一遍字符串。第二個find( )是帶int參數(shù)的,正如你所看到的,它會告訴方法從哪里開始找——即從參數(shù)位置開始查找。

Groups
Group是指里用括號括起來的,能被后面的表達式調用的正則表達式。Group 0 表示整個表達式,group 1表示第一個被括起來的group,以此類推。所以;

A(B(C))D里面有三個group:group 0是ABCD, group 1是BC,group 2是C。

你可以用下述Matcher方法來使用group:

public int groupCount( )返回matcher對象中的group的數(shù)目。不包括group0。

public String group( ) 返回上次匹配操作(比方說find( ))的group 0(整個匹配)

public String group(int i)返回上次匹配操作的某個group。如果匹配成功,但是沒能找到group,則返回null。

public int start(int group)返回上次匹配所找到的,group的開始位置。

public int end(int group)返回上次匹配所找到的,group的結束位置,最后一個字符的下標加一。

下面我們舉一些group的例子: //: c12:Groups.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
public class Groups {
 private static Test monitor = new Test();
 static public final String poem =
   "Twas brillig, and the slithy tovesn" +
   "Did gyre and gimble in the wabe.n" +
   "All mimsy were the borogoves,n" +
   "And the mome raths outgrabe.nn" +
   "Beware the Jabberwock, my son,n" +
   "The jaws that bite, the claws that catch.n" +
   "Beware the Jubjub bird, and shunn" +
   "The frumious Bandersnatch.";
 public static void main(String[] args) {
   Matcher m =
     Pattern.compile("(?m)(S+)s+((S+)s+(S+))$")
       .matcher(poem);
   while(m.find()) {
     for(int j = 0; j <= m.groupCount(); j++)
       System.out.print("[" + m.group(j) + "]");
     System.out.println();
   }
   monitor.expect(new String[]{
     "[the slithy toves]" +
     "[the][slithy toves][slithy][toves]",
     "[in the wabe.][in][the wabe.][the][wabe.]",
     "[were the borogoves,]" +
     "[were][the borogoves,][the][borogoves,]",
     "[mome raths outgrabe.]" +
     "[mome][raths outgrabe.][raths][outgrabe.]",
     "[Jabberwock, my son,]" +
     "[Jabberwock,][my son,][my][son,]",
     "[claws that catch.]" +
     "[claws][that catch.][that][catch.]",
     "[bird, and shun][bird,][and shun][and][shun]",
     "[The frumious Bandersnatch.][The]" +
     "[frumious Bandersnatch.][frumious][Bandersnatch.]"
   });
 }
} ///:~


這首詩是Through the Looking Glass的,Lewis Carroll的"Jabberwocky"的第一部分??梢钥吹竭@個正則表達式里有很多用括號括起來的group,它是由任意多個連續(xù)的非空字符('S+')和任意多個連續(xù)的空格字符('s+')所組成的,其最終目的是要捕獲每行的最后三個單詞;'$'表示一行的結尾。但是'$'通常表示整個字符串的結尾,所以這里要明確地告訴正則表達式注意換行符。這一點是由'(?m)'標志完成的(模式標志會過一會講解)。

start( )和end( )
如果匹配成功,start( )會返回此次匹配的開始位置,end( )會返回此次匹配的結束位置,即最后一個字符的下標加一。如果之前的匹配不成功(或者沒匹配),那么無論是調用start( )還是end( ),都會引發(fā)一個IllegalStateException。下面這段程序還演示了matches( )和lookingAt( ): //: c12:StartEnd.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
public class StartEnd {
 private static Test monitor = new Test();
 public static void main(String[] args) {
   String[] input = new String[] {
     "Java has regular expressions in 1.4",
     "regular expressions now expressing in Java",
     "Java represses oracular expressions"
   };
   Pattern
     p1 = Pattern.compile("rew*"),
     p2 = Pattern.compile("Java.*");
   for(int i = 0; i < input.length; i++) {
     System.out.println("input " + i + ": " + input[i]);
     Matcher
       m1 = p1.matcher(input[i]),
       m2 = p2.matcher(input[i]);
     while(m1.find())
       System.out.println("m1.find() '" + m1.group() +
         "' start = "+ m1.start() + " end = " + m1.end());
     while(m2.find())
       System.out.println("m2.find() '" + m2.group() +
         "' start = "+ m2.start() + " end = " + m2.end());
     if(m1.lookingAt()) // No reset() necessary
       System.out.println("m1.lookingAt() start = "
         + m1.start() + " end = " + m1.end());
     if(m2.lookingAt())
       System.out.println("m2.lookingAt() start = "
         + m2.start() + " end = " + m2.end());
     if(m1.matches()) // No reset() necessary
       System.out.println("m1.matches() start = "
         + m1.start() + " end = " + m1.end());
     if(m2.matches())
       System.out.println("m2.matches() start = "
         + m2.start() + " end = " + m2.end());
   }
   monitor.expect(new String[] {
     "input 0: Java has regular expressions in 1.4",
     "m1.find() 'regular' start = 9 end = 16",
     "m1.find() 'ressions' start = 20 end = 28",
     "m2.find() 'Java has regular expressions in 1.4'" +
     " start = 0 end = 35",
     "m2.lookingAt() start = 0 end = 35",
     "m2.matches() start = 0 end = 35",
     "input 1: regular expressions now " +
     "expressing in Java",
     "m1.find() 'regular' start = 0 end = 7",
     "m1.find() 'ressions' start = 11 end = 19",
     "m1.find() 'ressing' start = 27 end = 34",
     "m2.find() 'Java' start = 38 end = 42",
     "m1.lookingAt() start = 0 end = 7",
     "input 2: Java represses oracular expressions",
     "m1.find() 'represses' start = 5 end = 14",
     "m1.find() 'ressions' start = 27 end = 35",
     "m2.find() 'Java represses oracular expressions' " +
     "start = 0 end = 35",
     "m2.lookingAt() start = 0 end = 35",
     "m2.matches() start = 0 end = 35"
   });
 }
} ///:~


注意,只要字符串里有這個模式,find( )就能把它給找出來,但是lookingAt( )和matches( ),只有在字符串與正則表達式一開始就相匹配的情況下才能返回true。matches( )成功的前提是正則表達式與字符串完全匹配,而lookingAt( )[67]成功的前提是,字符串的開始部分與正則表達式相匹配。

匹配的模式(Pattern flags)
compile( )方法還有一個版本,它需要一個控制正則表達式的匹配行為的參數(shù):

Pattern Pattern.compile(String regex, int flag)flag的取值范圍如下: 編譯標志  效果  
Pattern.CANON_EQ  當且僅當兩個字符的"正規(guī)分解(canonical decomposition)"都完全相同的情況下,才認定匹配。比如用了這個標志之后,表達式"au030A"會匹配"?"。默認情況下,不考慮"規(guī)范相等性(canonical equivalence)"。  
Pattern.CASE_INSENSITIVE
(?i)  默認情況下,大小寫不明感的匹配只適用于US-ASCII字符集。這個標志能讓表達式忽略大小寫進行匹配。要想對Unicode字符進行大小不明感的匹配,只要將UNICODE_CASE與這個標志合起來就行了。  
Pattern.COMMENTS
(?x)  在這種模式下,匹配時會忽略(正則表達式里的)空格字符(譯者注:不是指表達式里的"s",而是指表達式里的空格,tab,回車之類)。注釋從#開始,一直到這行結束??梢酝ㄟ^嵌入式的標志來啟用Unix行模式。  
Pattern.DOTALL
(?s)  在這種模式下,表達式'.'可以匹配任意字符,包括表示一行的結束符。默認情況下,表達式'.'不匹配行的結束符。  
Pattern.MULTILINE
(?m)  在這種模式下,'^'和'$'分別匹配一行的開始和結束。此外,'^'仍然匹配字符串的開始,'$'也匹配字符串的結束。默認情況下,這兩個表達式僅僅匹配字符串的開始和結束。  
Pattern.UNICODE_CASE
(?u)  在這個模式下,如果你還啟用了CASE_INSENSITIVE標志,那么它會對Unicode字符進行大小寫不明感的匹配。默認情況下,大小寫不明感的匹配只適用于US-ASCII字符集。  
Pattern.UNIX_LINES
(?d)  在這個模式下,只有'n'才被認作一行的中止,并且與'.','^',以及'$'進行匹配。

在這些標志里面,Pattern.CASE_INSENSITIVE,Pattern.MULTILINE,以及Pattern.COMMENTS是最有用的(其中Pattern.COMMENTS還能幫我們把思路理清楚,并且/或者做文檔)。注意,你可以用在表達式里插記號的方式來啟用絕大多數(shù)的模式。這些記號就在上面那張表的各個標志的下面。你希望模式從哪里開始啟動,就在哪里插記號。

可以用"OR" ('|')運算符把這些標志合使用: //: c12:ReFlags.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
public class ReFlags {
 private static Test monitor = new Test();
 public static void main(String[] args) {
   Pattern p =  Pattern.compile("^java",
     Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
   Matcher m = p.matcher(
     "java has regexnJava has regexn" +
     "JAVA has pretty good regular expressionsn" +
     "Regular expressions are in Java");
   while(m.find())
     System.out.println(m.group());
   monitor.expect(new String[] {
     "java",
     "Java",
     "JAVA"
   });
 }
} ///:~


這樣創(chuàng)建出來的正則表達式就能匹配以"java","Java","JAVA"...開頭的字符串了。此外,如果字符串分好幾行,那它還會對每一行做匹配(匹配始于字符序列的開始,終于字符序列當中的行結束符)。注意,group( )方法僅返回匹配的部分。

split( )
所謂分割是指將以正則表達式為界,將字符串分割成String數(shù)組。

String[] split(CharSequence charseq)
String[] split(CharSequence charseq, int limit)這是一種既快又方便地將文本根據(jù)一些常見的邊界標志分割開來的方法。 //: c12:SplitDemo.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class SplitDemo {
 private static Test monitor = new Test();
 public static void main(String[] args) {
   String input =
     "This!!unusual use!!of exclamation!!points";
   System.out.println(Arrays.asList(
     Pattern.compile("!!").split(input)));
   // Only do the first three:
   System.out.println(Arrays.asList(
     Pattern.compile("!!").split(input, 3)));
   System.out.println(Arrays.asList(
     "Aha! String has a split() built in!".split(" ")));
   monitor.expect(new String[] {
     "[This, unusual use, of exclamation, points]",
     "[This, unusual use, of exclamation!!points]",
     "[Aha!, String, has, a, split(), built, in!]"
   });
 }
} ///:~


第二個split( )會限定分割的次數(shù)。

正則表達式是如此重要,以至于有些功能被加進了String類,其中包括split( )(已經(jīng)看到了),matches( ),replaceFirst( )以及replaceAll( )。這些方法的功能同Pattern和Matcher的相同。

替換操作
正則表達式在替換文本方面特別在行。下面就是一些方法:

replaceFirst(String replacement)將字符串里,第一個與模式相匹配的子串替換成replacement。

replaceAll(String replacement),將輸入字符串里所有與模式相匹配的子串全部替換成replacement。

appendReplacement(StringBuffer sbuf, String replacement)對sbuf進行逐次替換,而不是像replaceFirst( )或replaceAll( )那樣,只替換第一個或全部子串。這是個非常重要的方法,因為它可以調用方法來生成replacement(replaceFirst( )和replaceAll( )只允許用固定的字符串來充當replacement)。有了這個方法,你就可以編程區(qū)分group,從而實現(xiàn)更強大的替換功能。

調用完appendReplacement( )之后,為了把剩余的字符串拷貝回去,必須調用appendTail(StringBuffer sbuf, String replacement)。

下面我們來演示一下怎樣使用這些替換方法。說明一下,這段程序所處理的字符串是它自己開頭部分的注釋,是用正則表達式提取出來并加以處理之后再傳給替換方法的。 //: c12:TheReplacements.java
import java.util.regex.*;
import java.io.*;
import com.bruceeckel.util.*;
import com.bruceeckel.simpletest.*;
/*! Here's a block of text to use as input to
   the regular expression matcher. Note that we'll
   first extract the block of text by looking for
   the special delimiters, then process the
   extracted block. !*/
public class TheReplacements {
 private static Test monitor = new Test();
 public static void main(String[] args) throws Exception {
   String s = TextFile.read("TheReplacements.java");
   // Match the specially-commented block of text above:
   Matcher mInput =
     Pattern.compile("/*!(.*)!*/", Pattern.DOTALL)
       .matcher(s);
   if(mInput.find())
     s = mInput.group(1); // Captured by parentheses
   // Replace two or more spaces with a single space:
   s = s.replaceAll(" {2,}", " ");
   // Replace one or more spaces at the beginning of each
   // line with no spaces. Must enable MULTILINE mode:
   s = s.replaceAll("(?m)^ +", "");
   System.out.println(s);
   s = s.replaceFirst("[aeiou]", "(VOWEL1)");
   StringBuffer sbuf = new StringBuffer();
   Pattern p = Pattern.compile("[aeiou]");
   Matcher m = p.matcher(s);
   // Process the find information as you
   // perform the replacements:
   while(m.find())
     m.appendReplacement(sbuf, m.group().toUpperCase());
   // Put in the remainder of the text:
   m.appendTail(sbuf);
   System.out.println(sbuf);
   monitor.expect(new String[]{
     "Here's a block of text to use as input to",
     "the regular expression matcher. Note that we'll",
     "first extract the block of text by looking for",
     "the special delimiters, then process the",
     "extracted block. ",
     "H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tO",
     "thE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wE'll",
     "fIrst ExtrAct thE blOck Of tExt by lOOkIng fOr",
     "thE spEcIAl dElImItErs, thEn prOcEss thE",
     "ExtrActEd blOck. "
   });
 }
} ///:~


我們用前面介紹的TextFile.read( )方法來打開和讀取文件。mInput的功能是匹配'/*!' 和 '!*/' 之間的文本(注意一下分組用的括號)。接下來,我們將所有兩個以上的連續(xù)空格全都替換成一個,并且將各行開頭的空格全都去掉(為了讓這個正則表達式能對所有的行,而不僅僅是第一行起作用,必須啟用多行模式)。這兩個操作都用了String的replaceAll( )(這里用它更方便)。注意,由于每個替換只做一次,因此除了預編譯Pattern之外,程序沒有額外的開銷。

replaceFirst( )只替換第一個子串。此外,replaceFirst( )和replaceAll( )只能用常量(literal)來替換,所以如果你每次替換的時候還要進行一些操作的話,它們是無能為力的。碰到這種情況,你得用appendReplacement( ),它能讓你在進行替換的時候想寫多少代碼就寫多少。在上面那段程序里,創(chuàng)建sbuf的過程就是選group做處理,也就是用正則表達式把元音字母找出來,然后換成大寫的過程。通常你得在完成全部的替換之后才調用appendTail( ),但是如果要模仿replaceFirst( )(或"replace n")的效果,你也可以只替換一次就調用appendTail( )。它會把剩下的東西全都放進sbuf。

你還可以在appendReplacement( )的replacement參數(shù)里用"$g"引用已捕獲的group,其中'g' 表示group的號碼。不過這是為一些比較簡單的操作準備的,因而其效果無法與上述程序相比。

reset( )
此外,還可以用reset( )方法給現(xiàn)有的Matcher對象配上個新的CharSequence。 //: c12:Resetting.java
import java.util.regex.*;
import java.io.*;
import com.bruceeckel.simpletest.*;
public class Resetting {
 private static Test monitor = new Test();
 public static void main(String[] args) throws Exception {
   Matcher m = Pattern.compile("[frb][aiu][gx]")
     .matcher("fix the rug with bags");
   while(m.find())
     System.out.println(m.group());
   m.reset("fix the rig with rags");
   while(m.find())
     System.out.println(m.group());
   monitor.expect(new String[]{
     "fix",
     "rug",
     "bag",
     "fix",
     "rig",
     "rag"
   });
 }
} ///:~


如果不給參數(shù),reset( )會把Matcher設到當前字符串的開始處。

正則表達式與Java I/O
到目前為止,你看到的都是用正則表達式處理靜態(tài)字符串的例子。下面我們來演示一下怎樣用正則表達式掃描文件并且找出匹配的字符串。受Unix的grep啟發(fā),我寫了個JGrep.java,它需要兩個參數(shù):文件名,以及匹配字符串用的正則表達式。它會把匹配這個正則表達式那部分內容及其所屬行的行號打印出來。 //: c12:JGrep.java
// A very simple version of the "grep" program.
// {Args: JGrep.java "b[Ssct]w+"}
import java.io.*;
import java.util.regex.*;
import java.util.*;
import com.bruceeckel.util.*;
public class JGrep {
 public static void main(String[] args) throws Exception {
   if(args.length < 2) {
     System.out.println("Usage: java JGrep file regex");
     System.exit(0);
   }
   Pattern p = Pattern.compile(args[1]);
   // Iterate through the lines of the input file:
   ListIterator it = new TextFile(args[0]).listIterator();
   while(it.hasNext()) {
     Matcher m = p.matcher((String)it.next());
     while(m.find())
       System.out.println(it.nextIndex() + ": " +
         m.group() + ": " + m.start());
   }
 }
} ///:~


文件是用TextFile打開的(本章的前半部分講的)。由于TextFile會把文件的各行放在ArrayList里面,而我們又提取了一個ListIterator,因此我們可以在文件的各行當中自由移動(既能向前也可以向后)。

每行都會有一個Matcher,然后用find( )掃描。注意,我們用ListIterator.nextIndex( )跟蹤行號。

測試參數(shù)是JGrep.java和以[Ssct]開頭的單詞。

還需要StringTokenizer嗎?
看到正則表達式能提供這么強大的功能,你可能會懷疑,是不是還需要原先的StringTokenizer。JDK 1.4以前,要想分割字符串,只有用StringTokenizer。但現(xiàn)在,有了正則表達式之后,它就能做得更干凈利索了。 //: c12:ReplacingStringTokenizer.java
import java.util.regex.*;
import com.bruceeckel.simpletest.*;
import java.util.*;
public class ReplacingStringTokenizer {
 private static Test monitor = new Test();
 public static void main(String[] args) {
   String input = "But I'm not dead yet! I feel happy!";
   StringTokenizer stoke = new StringTokenizer(input);
   while(stoke.hasMoreElements())
     System.out.println(stoke.nextToken());
   System.out.println(Arrays.asList(input.split(" ")));
   monitor.expect(new String[] {
     "But",
     "I'm",
     "not",
     "dead",
     "yet!",
     "I",
     "feel",
     "happy!",
     "[But, I'm, not, dead, yet!, I, feel, happy!]"
   });
 }
} ///:~


有了正則表達式,你就能用更復雜的模式將字符串分割開來——要是交給StringTokenizer的話,事情會麻煩得多。我可以很有把握地說,正則表達式可以取代StringTokenizer。

“java正則表達式的實例用法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI