正则表达式判断是否只有汉字

正则表达式判断是否只有汉字,第1张

正则表达式判断是否只有汉字

文章目录
      • 需求,不为空 长度大于6 只包含汉字

需求,不为空 长度大于6 只包含汉字
package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class Test15 {


    
    public static final String STR_ENG_PATTERN = "^[\u4e00-\u9fa5]{0,}$";


    @Test
    public void lexiconTest() {

        boolean b7 = validateStrEnglish("");
        boolean b8 = validateStrEnglish("张三是暖宝宝的");
        boolean b9 = validateStrEnglish("张三是暖宝宝");

        System.out.println(b7);
        System.out.println(b8);
        System.out.println(b9);
    }

    
    public static boolean validateStrEnglish(final String str) {
        //判空
        if (StringUtils.isEmpty(str)) {
            return Boolean.FALSE;
        }
   
        boolean matches = str.matches(STR_ENG_PATTERN);
        if (str.length() <= 6) {
            return Boolean.FALSE;
        }
        if (matches) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5575780.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存