zoukankan      html  css  js  c++  java
  • 正则表达式

    什么是正则表达式?

    符合一定规则的表达式

    正则表达式的作用?

    专门用于操作字符串

    (sting的方法太复杂,代码多)

    string方法解决1

    package songyan;
    /*
     * 对qq号校验
     * 要求:
     * 5-15位(长度)
     * 0不能开头(startswith)
     * 只能拿是数字(array isdigit)
     * */
    public class test{
        public static void main(String[] args)
        {
            String qq="66666s";
            boolean flag=true;
            char[] arr=qq.toCharArray();
            if(qq.length()>=5&&qq.length()<=15)
            {
                if(!qq.startsWith("0"))
                {
                    for(char c : arr)
                    {
                        if(!(c>'0'&&c<'9'))
                        {
                            flag= false;
                            break;
                        }
                    }
                    if(flag)
                    {
                        
                        System.out.println("qq:"+qq);
                    }
                    else{
                        System.out.println("error");
                    }
                }
                else
                {
                    System.out.println("不能以0开头");
                }
            }
            else{
                System.out.println("长度错误");
            }
        }
    }

    string方法解决2

    package songyan;
    /*
     * 对qq号校验
     * 要求:
     * 5-15位(长度)
     * 0不能开头(startswith)
     * 只能拿是数字(array isdigit)
     * */
    public class test{
        public static void main(String[] args)
        {
            String qq="010085";
            try
            {
                Long.parseLong(qq);
                if(qq.length()>=5&&qq.length()<=15)
                {
                    if(!qq.startsWith("0"))
                    {
                        System.out.println("qq:"+qq);
                    }
                    else{
                        System.out.println("不能以0开头");
                    }
                }
                else
                {
                    System.out.println("长度错误");
                }
            
            }
            catch(NumberFormatException e)
            {
                System.out.println("出现非法字符");
            }
            
            
            
        }
    }

    正则写法

    package songyan;
    public class test{
        public static void main(String[] args)
        {
            String qq= "9j048";
            String format= "[1-9][0-9]{4,14}";
            boolean flag = qq.matches(format);
            if(flag)
            {
                System.out.println("qq:"+qq);            
            }
            else
            {
                System.out.println("error");
            }
        }
    }
  • 相关阅读:
    AngularJs 键盘事件和鼠标事件
    Linux的net.ipv4.tcp_timestamps参数
    实战:tcp链接rst场景tcpdump分析
    C++ map使用总结
    C++ 11常见功能介绍:auto,decltype,nullptr,for,lambda
    BLOCK层基本概念:bio,request,request_queue
    C: 字符数组中的空去掉
    代码静态分析工具-splint的学习与使用[转]
    代码分析工具splint安装介绍
    gcc/g++ 如何支持c11/c++11标准编译
  • 原文地址:https://www.cnblogs.com/exexex/p/8435618.html
Copyright © 2011-2022 走看看