正则表达式详解+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;
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'& gt;*</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'& gt;*</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. });
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. }
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}$