`
panfugen
  • 浏览: 25721 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

正则表达式详解+GWT-EXT实例

阅读更多

1先来看看正则表达式的基本语法:

        1.1普通字符:字母、数字、汉字、下划线以及没有被定义特殊意义的标点符号

        1.2简单的转义字符:一些不便书写的字符,比如换行符,制表符等,使用 \n,\t 来表示。另外有一些标点符号在正则表达式中,被定义了特殊的意义,因此需要在前面加 "\" 进行转义后,匹配该字符本身。

                   1.2.1简单列举一下常用的特殊意义的转义符,方便查看

                        ^   匹配输入字符串的开始位置,如匹配本身则加转义符\ ,即\^(下同)

                        $   匹配输入字符串的结束位置 

                        ()   标记一个子字符串的开始和结束位置 

                        []   用来自定义能够匹配"多种字符"的表达式

                       {}   修饰匹配的次数

                        .     修饰匹配除了换行符(\n)以外的任意一个字符

                        ?    修饰匹配次数为 0 次或 1 次

                        +    修饰匹配次数为至少 1 次

                        *    修饰匹配次数为 0 次或任意次

                        |     左右两边表达式之间 "或" 关系

                     1.2.2  以上特殊意义的转义符具有的共同特征是:匹配本身均为加上转义符"\"

                     1.2.3  常用标准字符表达集合

                        .    小数点可以匹配除了换行符(\n)以外的任意一个字符

                        \w 可以匹配任何一个字母或者数字或者下划线(注w小写)

                        \W 可以匹配任何一个字母或者数字或者下划线以外的字符(注W大写)

                        \d  可以匹配0-9中的任意一个数字字符

                        \D  D大写可以匹配任何一个非数字字符

                    1.2.4  自定义字符集合

                       用中括号 [ ] 包含多个字符,可以匹配所包含的字符中的任意一个。同样,每次只能匹配其中一个

                       用中括号 [^ ] 包含多个字符,构成否定格式,可以匹配所包含的字符之外的任意一个字符。    

                            注:

                                    正则表达式中的特殊符号,如果被包含于中括号中,则失去特殊意义,但 \ [ ] : ^ - 除外。

                                    标准字符集合,除小数点(.)外,如果被包含于中括号中,自定义字符集合将包含该集合。
                                    比如:
[\d.\-+],将可以匹配数字,小数点和 + - 符号。(小数点和 + 号失去特殊意义)

                                    用减号相连的 2 个普通字符,自定义字符集合将包含该范围。
                                    比如:
[\dA-Fa-f],将可以匹配 0 - 9, A - F, a - f

2下面结合GWT-EXT的NumberField实现提示信息的自定义

    定义全局变量

 

Java代码 复制代码
  1. // 单价   
  2.    private NumberField unitPrice;   
  3.   
  4.    // 工艺费   
  5.    private NumberField craftFee;   
  6. // 石重量   
  7.    private NumberField stoneWeight;   
  8.   
  9.    // 总重   
  10.    private NumberField totalWeight;  
 // 单价
    private NumberField unitPrice;

    // 工艺费
    private NumberField craftFee;
 // 石重量
    private NumberField stoneWeight;

    // 总重
    private NumberField totalWeight;

    初始化

Java代码 复制代码
  1.  // 石重量   
  2.         this.stoneWeight = new NumberField();   
  3.         this.stoneWeight.setFieldLabel("石重(克拉)");   
  4.         this.stoneWeight.setMaxLength(10);   
  5. //响应光标离开输入框时的事件   
  6.         this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {   
  7.             public void handleEvent(BaseEvent be) {   
  8.                 setFormat(stoneWeight, stoneWeight.getRawValue());   
  9.             }   
  10.         }); // 单价   
  11.         this.unitPrice = new NumberField();   
  12.         this.unitPrice.setFieldLabel("<font color='red'>*</font>单价");   
  13.         this.unitPrice.setAllowDecimals(true);   
  14.         this.unitPrice.setAllowBlank(false);   
  15.         this.unitPrice.setMaxLength(10);   
  16.         this.unitPrice.setFormat(NumberFormat.getDecimalFormat());   
  17.         this.unitPrice.addListener(Events.OnBlur, new Listener<BaseEvent>() {   
  18.             public void handleEvent(BaseEvent be) {   
  19.                 autoSetFormat(unitPrice, unitPrice.getRawValue());   
  20.             }   
  21.         });   
  22.   
  23.         // 工艺费   
  24.         this.craftFee = new NumberField();   
  25.         this.craftFee.setFieldLabel("<font color='red'>*</font>工艺费");   
  26.         this.craftFee.setAllowDecimals(true);   
  27.         this.craftFee.setAllowBlank(false);   
  28.         this.craftFee.setMaxLength(10);   
  29.         this.craftFee.setFormat(NumberFormat.getDecimalFormat());         
  30.         this.craftFee.addListener(Events.OnBlur, new Listener<BaseEvent>() {   
  31.             public void handleEvent(BaseEvent be) {   
  32.                 autoSetFormat(craftFee, craftFee.getRawValue());   
  33.             }   
  34.         });   
  35.   
  36.         // 石重量   
  37.         this.stoneWeight = new NumberField();   
  38.         this.stoneWeight.setFieldLabel("石重(克拉)");   
  39.         this.stoneWeight.setMaxLength(10);   
  40.         this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {   
  41.             public void handleEvent(BaseEvent be) {   
  42.                 setFormat(stoneWeight, stoneWeight.getRawValue());   
  43.             }   
  44.         });   
  45.   
  46.         // 总重   
  47.         this.totalWeight = new NumberField();   
  48.         this.totalWeight.setFieldLabel("总重(克)");   
  49.         this.totalWeight.setMaxLength(10);   
  50.         this.totalWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {   
  51.             public void handleEvent(BaseEvent be) {   
  52.                 setFormat(totalWeight, totalWeight.getRawValue());   
  53.             }   
  54.         });  
 // 石重量
        this.stoneWeight = new NumberField();
        this.stoneWeight.setFieldLabel("石重(克拉)");
        this.stoneWeight.setMaxLength(10);
//响应光标离开输入框时的事件
        this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
            public void handleEvent(BaseEvent be) {
                setFormat(stoneWeight, stoneWeight.getRawValue());
            }
        }); // 单价
        this.unitPrice = new NumberField();
        this.unitPrice.setFieldLabel("<font color='red'>*</font>单价");
        this.unitPrice.setAllowDecimals(true);
        this.unitPrice.setAllowBlank(false);
        this.unitPrice.setMaxLength(10);
        this.unitPrice.setFormat(NumberFormat.getDecimalFormat());
        this.unitPrice.addListener(Events.OnBlur, new Listener<BaseEvent>() {
            public void handleEvent(BaseEvent be) {
                autoSetFormat(unitPrice, unitPrice.getRawValue());
            }
        });

        // 工艺费
        this.craftFee = new NumberField();
        this.craftFee.setFieldLabel("<font color='red'>*</font>工艺费");
        this.craftFee.setAllowDecimals(true);
        this.craftFee.setAllowBlank(false);
        this.craftFee.setMaxLength(10);
        this.craftFee.setFormat(NumberFormat.getDecimalFormat());      
        this.craftFee.addListener(Events.OnBlur, new Listener<BaseEvent>() {
            public void handleEvent(BaseEvent be) {
                autoSetFormat(craftFee, craftFee.getRawValue());
            }
        });

        // 石重量
        this.stoneWeight = new NumberField();
        this.stoneWeight.setFieldLabel("石重(克拉)");
        this.stoneWeight.setMaxLength(10);
        this.stoneWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
            public void handleEvent(BaseEvent be) {
                setFormat(stoneWeight, stoneWeight.getRawValue());
            }
        });

        // 总重
        this.totalWeight = new NumberField();
        this.totalWeight.setFieldLabel("总重(克)");
        this.totalWeight.setMaxLength(10);
        this.totalWeight.addListener(Events.OnBlur, new Listener<BaseEvent>() {
            public void handleEvent(BaseEvent be) {
                setFormat(totalWeight, totalWeight.getRawValue());
            }
        });

   响应事件的方法

 

Java代码 复制代码
  1. /**  
  2.  * 自动设置数字的格式  
  3.  * 如输入的为整数则自动加上两位小数  
  4.  * 如输入的带有小数则验证是否为两位小数,不是则报错  
  5.  * @param f为NumberField  
  6.  * @param value输入值  
  7.  */  
  8.     private static void autoSetFormat(NumberField f, String value) {   
  9.         if (!value.equals("")) {   
  10.             if (value.indexOf(".") == -1) {   
  11.                 value += ".00";   
  12.                 f.setRawValue(value);   
  13.                 f.clearInvalid();   
  14.             } else {   
  15.                 f.setRegex("^[\\d]*[\\.][\\d][\\d]$");   
  16.                 f.getMessages().setRegexText("小数点后保留两位");   
  17.             }   
  18.         }   
  19.     }   
  20. /**  
  21.  * 自动设置数字的格式  
  22.  * 如输入的为整数则自动加上三位小数  
  23.  * 如输入的带有小数则验证是否为三位小数,不是则报错  
  24.  * @param f为NumberField  
  25.  * @param value输入值  
  26.  */  
  27.        
  28.     private static void setFormat(NumberField f, String value) {   
  29.         if (!value.equals("")) {   
  30.             if (value.indexOf(".") == -1) {   
  31.                 value += ".000";   
  32.                 f.setRawValue(value);   
  33.                 f.clearInvalid();   
  34.             } else {   
  35.                 f.setRegex("^[\\d]*[\\.][\\d]{3}$");   
  36.                 f.getMessages().setRegexText("小数点后保留三位");   
  37.             }   
  38.         }   
  39.   
  40.     }  
/**
 * 自动设置数字的格式
 * 如输入的为整数则自动加上两位小数
 * 如输入的带有小数则验证是否为两位小数,不是则报错
 * @param f为NumberField
 * @param value输入值
 */
    private static void autoSetFormat(NumberField f, String value) {
        if (!value.equals("")) {
            if (value.indexOf(".") == -1) {
                value += ".00";
                f.setRawValue(value);
                f.clearInvalid();
            } else {
                f.setRegex("^[\\d]*[\\.][\\d][\\d]$");
                f.getMessages().setRegexText("小数点后保留两位");
            }
        }
    }
/**
 * 自动设置数字的格式
 * 如输入的为整数则自动加上三位小数
 * 如输入的带有小数则验证是否为三位小数,不是则报错
 * @param f为NumberField
 * @param value输入值
 */
    
    private static void setFormat(NumberField f, String value) {
        if (!value.equals("")) {
            if (value.indexOf(".") == -1) {
                value += ".000";
                f.setRawValue(value);
                f.clearInvalid();
            } else {
                f.setRegex("^[\\d]*[\\.][\\d]{3}$");
                f.getMessages().setRegexText("小数点后保留三位");
            }
        }

    }

   效果图在附件中


   附

电子邮件

 

              ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$

汉字(限定只能输入m-n个汉字,m,n值可以修改)

              ^[\u4e00-\u9fa5]{m,n}$

 

 

 

 

 

                                  

 

  • F31cf205-a1f0-30b5-8fb8-cbd5d3747b3e-thumb
  • 大小: 7.4 KB
  • 15d9b98f-3cf5-3934-916b-4a02a2e1a1e5-thumb
  • 大小: 6.1 KB
分享到:
评论

相关推荐

    Gwt-Ext学习笔记之进级篇

    GWT-Ext 是基于 Google Web Toolkit(GWT)和 ExtJs 的功能强大的网页开发控件库。它非常适用于进行纯 Java 语言的富 Internet 应用的快速开发。本系列文章将详细讲解 GWT-Ext 的基本结构和功能特点,并通过代码示例...

    Gwt-ext学习笔记之基础篇

    Gwt-ext学习笔记之基础篇、安装、配置、例子。

    最好的gwt-ext学习资料

    最好的gwt-ext学习资料,适合初学者。

    非常好的gwt-ext培训教程

    GWT-Ext 是基于 Google Web Toolkit(GWT)和 ExtJs 的功能强大的网页开发控件库。它扩展了 GWT,在 ExtJs 的基础上实现了有排序功能的表格(Grid)、分页、过滤,支持有拖拽功能的树,高度可定制的组合下拉框...

    GWT-ext 布局示例

    GWT-Ext 是基于 Google Web Toolkit(GWT)和 ExtJs 的功能强大的网页开发控件库。它非常适用于进行纯 Java 语言的富 Internet 应用的快速开发。本系列文章将详细讲解 GWT-Ext 的基本结构和功能特点,并通过代码示例...

    gwt-ext-选课系统

    gwt-ext-选课系统gwt-ext-选课系统gwt-ext-选课系统gwt-ext-选课系统

    Gwt-ext学习笔记

    Gwt-ext学习笔记,Gwt-ext学习笔记,Gwt-ext学习笔记

    GWT-Ext 控件演示

    GWT-Ext 控件演示 GWT-Ext 控件演示

    需要GWT-Ext 2.0.5来下

    GWT-Ext 2.0.5 has been released. Grab the distribution from the Downloads area. GWT-Ext 2.0.5 supports GWT 1.5 final (1.5.2), Firefox3 and Chrome.

    gwt-ext-tree

    gwt-ext-treegwt-ext-treegwt-ext-treegwt-ext-treegwt-ext-tree

    GWT-EXT2.0最佳实践教程,源代码打包供下载

    NULL 博文链接:https://kenter1643.iteye.com/blog/323084

    GWT-Ext资料整理.chm

    个人学习收集网上资料辛苦整理而成; GWT-Ext 整理 chm java js GWT-Ext资料整理.chm

    GWT-Ext体验之旅

    这是一篇非常好的GWT-Ext的文章,讲解非常详细,从入门的讲解示例再到精通一应俱全,学习GWT-Ext的朋友可以看看

    GWT-Ext_体验之旅.doc

    GWT-Ext_体验之旅.doc GWT-Ext_体验之旅.doc GWT-Ext_体验之旅.doc

    gwt-ext explorer eclipse project 入门与提高

    各种各样的资源找齐还是要点时间的: 1、下载gwt-ext2.04解压 2、解压本资源,导入Eclipse 3、将解压gwt-ext2.04得到一jar包设入classpath 4、按载图编辑你的src 文件夹

    gwt-ex t学习必备资料

    gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-ex t学习必备资料gwt ext gwt-ext gwt-...

    gwt-ext 实例

    NULL 博文链接:https://shiyan31214.iteye.com/blog/774191

    gwt-ext相关jar包及资源

    gwt-ext相关jar包及资源

Global site tag (gtag.js) - Google Analytics