想用正则表达式匹配汉字,就是匹配 “张”,“赵”,“王”,这些姓的表达式,应该怎么写啊?java里怎么写?

解决方案 »

  1.   

    判断汉字的正则: [\u4e00-\u9fa5]
      

  2.   

    一般情况下,[\u4e00-\u9fa5] 可以解决
      

  3.   

    Java采用的是UNICODE,所以直接写就可以了import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class Test{
    public static void main(String[] args){
    String regex = "(张|赵|王)";
    Pattern pattern = Pattern.compile(regex);
    String text = "王小丫";
    Matcher matcher = pattern.matcher(text);
    if(matcher.find()){
    System.out.println("找到了: " + matcher.group(1));
    }else{
    System.out.println("找不到");
    }
    }
    }
      

  4.   

    (*^__^*) 嘻嘻……老外不知道,但是我们可以百度哦·~呵呵,google就免了,因为最近中国在和谐掉他·~哈哈
      

  5.   

    百度?从来不用的就算不用 Google,我也不会用百度。拿 Struts 这个关键字来说,看看在百度上搜到的都是些什么?而在 Google、有道等地方搜到的第一个就是 Struts 的官方网站。像百度这种利欲薰心的做法,实在让人厌恶!
      

  6.   

    呵呵~~其实我个人以前很喜欢百度,但是现在非常喜欢google,百度太什么了。而且学唐骏说的:拿个老二老三(百度)加起来也比不过一个google。
      

  7.   

    我想匹配姓是第一个字:
    public static void main(String[] args){
            String regex = "(张|赵|王)";
            Pattern pattern = Pattern.compile(regex);
            String text = "小丫王";
            Matcher matcher = pattern.matcher(text);
            if(matcher.find()){
                System.out.println("找到了: " + matcher.group(1));
            }else{
                System.out.println("找不到");
            }
        }
    }也可找到,能不能让这种情况不出现啊?
      

  8.   


    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class Test{
        public static void main(String[] args){
            String regex = "^(张|赵|王)";
            Pattern pattern = Pattern.compile(regex);
            String text = "王小丫";
            Matcher matcher = pattern.matcher(text);
            if(matcher.find()){
                System.out.println("找到了: " + matcher.group(1));
            }else{
                System.out.println("找不到");
            }
        }
    }
      

  9.   


    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class Test{
        public static void main(String[] args){
            String regex = "^(张|赵|王)";
            Pattern pattern = Pattern.compile(regex);
            String text = "王小丫";
            Matcher matcher = pattern.matcher(text);
            if(matcher.find()){
                System.out.println("找到了: " + matcher.group(1));
            }else{
                System.out.println("找不到");
            }
        }
    }这样就可以了
      

  10.   


    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class Test{
        public static void main(String[] args){
            String regex = "^(张|赵|王)";
            Pattern pattern = Pattern.compile(regex);
            String text = "王小丫";
            Matcher matcher = pattern.matcher(text);
            if(matcher.find()){
                System.out.println("找到了: " + matcher.group(1));
            }else{
                System.out.println("找不到");
            }
        }
    }