JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:取代成大/小寫 [Re:kebin_liu]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2006-09-21 00:42 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
My way to do it ... ( more general )
Replace simply "wrap" the appendReplacement and appendTrail.
Need JDK >= 1.5
( Add replaceFirst yourself if you needed )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.regex.*;
 
public class Replace {
  public static interface Block {
    public CharSequence lambda(MatchResult match);  // Do not return null.
  }
 
  public static CharSequence replaceAll(CharSequence source, String regex, Replace.Block code) {
    return replaceAll(source, Pattern.compile(regex), code);
  }
 
  public static CharSequence replaceAll(CharSequence source, Pattern regex, Replace.Block code) {
    if ((source == null) || (regex == null) || (code == null)) {
      throw new IllegalArgumentException("Arguments cannot be null.");
    }
    
    Matcher matcher = regex.matcher(source);
    if (!matcher.find()) {
      return source;
    } else {
      matcher.reset();
      StringBuffer result = new StringBuffer();
      while (matcher.find()) {
        matcher.appendReplacement(result, code.lambda(matcher.toMatchResult()).toString());
      }
      matcher.appendTail(result);
      return result;
    }
  }
  
  // some tests to show how to use ...
  public static void main(String[] args) {
    System.out.println(Replace.replaceAll("test 123 hello world, java a b c ", "\\w+", new Replace.Block() { 
      public CharSequence lambda(MatchResult match) { 
        String s = match.group();
        return s.substring(0,1).toUpperCase() + ((s.length() > 1) ? s.substring(1) : "");
      }
    }));
 
    System.out.println(Replace.replaceAll("java java java java java", "java", new Replace.Block() { 
      int count = 0;
      public CharSequence lambda(MatchResult match) { 
        return (++count != 5) ? "perl" : "ruby";
      }
    }));
    
    System.out.println(Replace.replaceAll("Replace word by their length ...", "[^ ]+", new Replace.Block() { 
      public CharSequence lambda(MatchResult match) { 
        return String.valueOf(match.group().length());
      }
    }));
 
    System.out.println(Replace.replaceAll("%5s text %9s text", "([^ ]+)\\s+(\\w+)", new Replace.Block() { 
      public CharSequence lambda(MatchResult match) { 
        return String.format(match.group(1), match.group(2));
      }
    }));
  }
}


T55555 edited on 2006-09-21 01:29
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
4038 取代成大/小寫 kebin_liu 4403 2006-09-19 17:52
3742 Re:取代成大/小寫 T55555 2309 2006-09-21 00:42
» JWorld@TW »  Java 程式分享區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8