隨筆 - 17  文章 - 84  trackbacks - 0
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          如非特別說明,所有文章均為原創(chuàng)。如需引用,請注明出處
          Email:liangtianyu@gmail.com
          MSN:terry.liangtianyu@hotmail.com

          常用鏈接

          留言簿(4)

          隨筆分類(12)

          隨筆檔案(17)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 51902
          • 排名 - 962

          最新評論

          閱讀排行榜

          評論排行榜

          下面是我總結出來的Lucene中對字符類型的判斷正則表達式:

          用于判斷Unicode Letter:

                String UnicodeLetterPattern = "[(\u0041-\u005a)|(\u0061-\u007a)|(\u00c0-\u00d6)|(\u00d8-\u00f6)|(\u00f8-\u00ff)|(\u0100-\u1fff)]";

          用于判斷亞洲語言字符(中國,日本,韓國):

                  String UnicodeCJPattern = "[(\u3040-\u318f)|(\u3300-\u337f)|(\u3400-\u3d2d)|(\u4e00-\u9fff)|(\uf900-\ufaff)|(\uac00-\ud7af)]";

          用于判斷Unicode中的數(shù)字:

                  String UnicodeDigitPattern = "[(\u0030-\u0039)|(\u0660-\u0669)|(\u06f0-\u06f9)|(\u0966-\u096f)|(\u09e6-\u09ef)|(\u0a66-\u0a6f)|(\u0ae6-\u0aef)|(\u0b66-\u0b6f)|(\u0be7-\u0bef)|(\0c66-\u0c6f)|(\u0ce6-\u0cef)|(\u0d66-\u0d6f)|(\u0e50-\u0e59)|(\u0ed0-\u0ed9)|(\u1040-\u1049)]";
          posted on 2007-07-02 08:14 Terry Liang 閱讀(1597) 評論(5)  編輯  收藏 所屬分類: Lucene 2.1研究

          FeedBack:
          # re: Lucene 2.1研究:對字符的判斷 2007-07-02 14:02 xmlspy
          沒弄明白你這個到底如何用,下面是我的測試代碼
          無論如何都是返回false
           
           1 import org.apache.oro.text.regex.MalformedPatternException;
           2 import org.apache.oro.text.regex.Pattern;
           3 import org.apache.oro.text.regex.PatternCompiler;
           4 import org.apache.oro.text.regex.PatternMatcher;
           5 import org.apache.oro.text.regex.Perl5Compiler;
           6 import org.apache.oro.text.regex.Perl5Matcher;
           7 
           8 //正則表達式
           9 public class RegxLan {
          10 
          11     //用于判斷Unicode Letter:
          12     private static final String UNICODE_LETTER_PATTERN = "[(\u0041-\u005a)|"
          13             + "(\u0061-\u007a)|(\u00c0-\u00d6)|(\u00d8-\u00f6)|(\u00f8-\u00ff)|"
          14             + "(\u0100-\u1fff)]";
          15 
          16     //用于判斷亞洲語言字符(中國,日本,韓國):
          17     private static final String UNICODE_CJP_PATTERN = "[(\u3040-\u318f)|(\u3300-\u337f)|"
          18             + "(\u3400-\u3d2d)|(\u4e00-\u9fff)|(\uf900-\ufaff)|(\uac00-\ud7af)]";
          19 
          20     //用于判斷Unicode中的數(shù)字:
          21     private static final String UNICODE_DIGIT_PATTERN = "[(\u0030-\u0039)|"
          22             + "(\u0660-\u0669)|(\u06f0-\u06f9)|(\u0966-\u096f)|(\u09e6-\u09ef)|"
          23             + "(\u0a66-\u0a6f)|(\u0ae6-\u0aef)|(\u0b66-\u0b6f)|(\u0be7-\u0bef)|"
          24             + "(\0c66-\u0c6f)|(\u0ce6-\u0cef)|(\u0d66-\u0d6f)|(\u0e50-\u0e59)|"
          25             + "(\u0ed0-\u0ed9)|(\u1040-\u1049)]";
          26 
          27     /**
          28      * 判斷是否是Unicode字母
          29      */
          30     public static final boolean isUnicodeLetter(String str) {
          31         return testString(str,UNICODE_LETTER_PATTERN);
          32     }
          33     /**
          34      * 判斷是否是Unicode數(shù)字
          35      */
          36     public static final boolean isUnicodeDigit(String str) {
          37         return testString(str,UNICODE_DIGIT_PATTERN);
          38     }
          39     /**
          40      * 判斷是否是Unicode亞洲語言字符
          41      */
          42     public static final boolean isUnicodeCPJ(String str) {
          43         return testString(str,UNICODE_CJP_PATTERN);
          44     }
          45 
          46     public static void main(String[] args) {
          47         String x="123";
          48         boolean is=isUnicodeLetter(x);
          49         System.out.println(is);
          50         is=isUnicodeDigit(x);
          51         System.out.println(is);
          52         is=isUnicodeCPJ(x);
          53         System.out.println(is);
          54     }
          55     private static final boolean testString(String str, String pattern) {
          56         PatternCompiler cpl = new Perl5Compiler();
          57         Pattern p=null;
          58         try {
          59             p=cpl.compile(pattern);
          60         } catch (MalformedPatternException e) {
          61             e.printStackTrace();
          62         }
          63         PatternMatcher matcher=new Perl5Matcher();
          64         return matcher.matches(str, p);
          65     }
          66 }
          67 
            回復  更多評論
            
          # re: Lucene 2.1研究:對字符的判斷 2007-07-02 14:16 Terry Liang
          @xmlspy
          我定義的是正則表達式樣式,我在C#中測試通過,而且我已經(jīng)指明是判斷單個字符的,假如傳入字符串,當然只會返回false了。
          例如:對于“我”,假如UnicodeCJPattern去正則匹配,則會返回true。
          很不好意思,我沒有寫一個java正則表達式應用的事例。
            回復  更多評論
            
          # re: Lucene 2.1研究:對字符的判斷 2007-07-02 21:37 xmlspy
          謝謝 :)

          把我那個改了吧,正好當作示例用 :)  回復  更多評論
            
          # re: Lucene 2.1研究:對字符的判斷 2007-07-02 22:24 xmlspy
          測試了一下,還是有些問題的,不嚴謹.
          哥們請看一下 :)
           
           1 import org.apache.oro.text.regex.MalformedPatternException;
           2 import org.apache.oro.text.regex.Pattern;
           3 import org.apache.oro.text.regex.PatternCompiler;
           4 import org.apache.oro.text.regex.PatternMatcher;
           5 import org.apache.oro.text.regex.Perl5Compiler;
           6 import org.apache.oro.text.regex.Perl5Matcher;
           7 
           8 //正則表達式
           9 //jdk版本:jdk1.5.0_09
          10 //類庫:jakarta-oro-2.0.8.jar
          11 //操作系統(tǒng): win2003 standard
          12 public class RegxLan {
          13 
          14     //用于判斷Unicode Letter:
          15     private static final String UNICODE_LETTER_PATTERN = "[(\u0041-\u005a)|"
          16             + "(\u0061-\u007a)|(\u00c0-\u00d6)|(\u00d8-\u00f6)|(\u00f8-\u00ff)|"
          17             + "(\u0100-\u1fff)]";
          18 
          19     //用于判斷亞洲語言字符(中國,日本,韓國):
          20     private static final String UNICODE_CJP_PATTERN = "[(\u3040-\u318f)|(\u3300-\u337f)|"
          21             + "(\u3400-\u3d2d)|(\u4e00-\u9fff)|(\uf900-\ufaff)|(\uac00-\ud7af)]";
          22 
          23     //用于判斷Unicode中的數(shù)字:
          24     private static final String UNICODE_DIGIT_PATTERN = "[(\u0030-\u0039)|"
          25             + "(\u0660-\u0669)|(\u06f0-\u06f9)|(\u0966-\u096f)|(\u09e6-\u09ef)|"
          26             + "(\u0a66-\u0a6f)|(\u0ae6-\u0aef)|(\u0b66-\u0b6f)|(\u0be7-\u0bef)|"
          27             + "(\0c66-\u0c6f)|(\u0ce6-\u0cef)|(\u0d66-\u0d6f)|(\u0e50-\u0e59)|"
          28             + "(\u0ed0-\u0ed9)|(\u1040-\u1049)]";
          29 
          30     /**
          31      * 判斷是否是Unicode字母
          32      */
          33     public static final boolean isUnicodeLetter(String str) {
          34         return testString(str, UNICODE_LETTER_PATTERN);
          35     }
          36 
          37     /**
          38      * 判斷是否是Unicode數(shù)字
          39      */
          40     public static final boolean isUnicodeDigit(String str) {
          41         return testString(str, UNICODE_DIGIT_PATTERN);
          42     }
          43 
          44     /**
          45      * 判斷是否是Unicode亞洲語言字符
          46      */
          47     public static final boolean isUnicodeCPJ(String str) {
          48         return testString(str, UNICODE_CJP_PATTERN);
          49     }
          50 
          51     //通過測試,看到還是有問題的,尤其是對符號判讀不正確,
          52     //另外,把英文字母也當作數(shù)字對待了
          53     //全角字符,和.返回的都是false,而全角字符×返回的確實false,true,false
          54     //
          55     public static void main(String[] args) {
          56         //最后三個是全角字符
          57         char[] test = "`~!@#$%^&*()_-+=|\\,.<>/?;:'\"[]{}w2這×,.".toCharArray();
          58 
          59         for (char t : test) {
          60             String x = String.valueOf(t);
          61             System.out.println("========== 字符: "+t+" 的結果 ==========");
          62 
          63             boolean is = isUnicodeLetter(x);
          64             System.out.println("isUnicodeLetter == "+is);
          65             is = isUnicodeDigit(x);
          66             System.out.println("isUnicodeDigit == "+is);
          67             is = isUnicodeCPJ(x);
          68             System.out.println("isUnicodeCPJ == "+is);
          69         }
          70     }
          71 
          72     private static final boolean testString(String str, String pattern) {
          73         PatternCompiler cpl = new Perl5Compiler();
          74         Pattern p = null;
          75         try {
          76             p = cpl.compile(pattern);
          77         } catch (MalformedPatternException e) {
          78             e.printStackTrace();
          79         }
          80         PatternMatcher matcher = new Perl5Matcher();
          81         return matcher.matches(str, p);
          82     }
          83 }
          84 
            回復  更多評論
            
          # re: Lucene 2.1研究:對字符的判斷 2007-07-18 12:43 Terry Liang
          @xmlspy
          我不了解java和.net對正則表達式的應用有什么異同。
          上述判斷證則表示樣式我只在.net中測試過。
          @xmlspy能否告訴我具體有什不嚴謹?shù)牡胤侥兀?
            回復  更多評論
            
          主站蜘蛛池模板: 平谷区| 安溪县| 中西区| 双柏县| 寻乌县| 仙桃市| 孙吴县| 湘潭县| 岳阳县| 临湘市| 新田县| 阿坝县| 漳平市| 绵阳市| 临武县| 南雄市| 乌兰浩特市| 大连市| 大理市| 庆安县| 乡城县| 黑山县| 濮阳县| 吐鲁番市| 德清县| 岐山县| 台安县| 新沂市| 隆昌县| 离岛区| 通河县| 五华县| 竹山县| 开江县| 钦州市| 宜宾县| 江门市| 芜湖县| 伊川县| 连城县| 滦平县|