Skip to content

Commit 634a90b

Browse files
update
1 parent dba631d commit 634a90b

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>常用正则表达式</title>
6+
</head>
7+
<body>
8+
<h1>常用正则表达式</h1>
9+
10+
1 数字:^[0-9]*$
11+
12+
2 n位的数字:^\d{n}$
13+
14+
3 至少n位的数字:^\d{n,}$
15+
16+
4 m-n位的数字:^\d{m,n}$
17+
18+
5 零和非零开头的数字:^(0|[1-9][0-9]*)$
19+
20+
6 非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$
21+
22+
7 带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$
23+
24+
8 正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
25+
26+
9 有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
27+
28+
10 有1~3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
29+
30+
11 非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
31+
32+
12 非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
33+
34+
13 非负整数:^\d+$ 或 ^[1-9]\d*|0$
35+
36+
14 非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$
37+
38+
15 非负浮点数:^\d+(\.\d+)?$ 或 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
39+
40+
16 非正浮点数:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ 或 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
41+
42+
17 正浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 或 ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
43+
44+
18 负浮点数:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 或 ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
45+
46+
19 浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
47+
48+
二、校验字符的表达式
49+
50+
1 汉字:^[\u4e00-\u9fa5]{0,}$
51+
52+
2 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
53+
54+
3 长度为3-20的所有字符:^.{3,20}$
55+
56+
4 由26个英文字母组成的字符串:^[A-Za-z]+$
57+
58+
5 由26个大写英文字母组成的字符串:^[A-Z]+$
59+
60+
6 由26个小写英文字母组成的字符串:^[a-z]+$
61+
62+
7 由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
63+
64+
8 由数字、26个英文字母或者下划线组成的字符串:^\w+$ 或 ^\w{3,20}$
65+
66+
9 中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$
67+
68+
10 中文、英文、数字但不包括下划线等符号:^[\u4E00-\u9FA5A-Za-z0-9]+$ 或 ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
69+
70+
11 可以输入含有^%&',;=?$\"等字符:[^%&',;=?$\x22]+
71+
72+
12 禁止输入含有~的字符:[^~\x22]+
73+
74+
三、特殊需求表达式
75+
76+
1 Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
77+
78+
2 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
79+
80+
3 InternetURL:[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
81+
82+
4 手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
83+
84+
5 电话号码("XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX):^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
85+
86+
6 国内电话号码(0511-4405222、021-87888822):\d{3}-\d{8}|\d{4}-\d{7}
87+
88+
7 身份证号(15位、18位数字):^\d{15}|\d{18}$
89+
90+
8 短身份证号码(数字、字母x结尾):^([0-9]){7,18}(x|X)?$ 或 ^\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$
91+
92+
9 帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
93+
94+
10 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线):^[a-zA-Z]\w{5,17}$
95+
96+
11 强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间):^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
97+
98+
12 日期格式:^\d{4}-\d{1,2}-\d{1,2}
99+
100+
13 一年的12个月(01~09和1~12):^(0?[1-9]|1[0-2])$
101+
102+
14 一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$
103+
104+
15 钱的输入格式:有四种钱的表示形式我们可以接受:"10000.00" 和 "10,000.00", 和没有 "分" 的 "10000" 和 "10,000":^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$
105+
106+
16 xml文件:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
107+
108+
17 中文字符的正则表达式:[\u4e00-\u9fa5]
109+
110+
18 双字节字符:[^\x00-\xff] (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1))
111+
112+
19 空白行的正则表达式:\n\s*\r (可以用来删除空白行)
113+
114+
20 HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? /> (网上流传的版本太糟糕,上面这个也仅仅能部分,对于复杂的嵌套标记依旧无能为力)
115+
116+
21 首尾空白字符的正则表达式:^\s*|\s*$或(^\s*)|(\s*$) (可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式)
117+
118+
22 腾讯QQ号:[1-9][0-9]{4,} (腾讯QQ号从10000开始)
119+
120+
23 中国邮政编码:[1-9]\d{5}(?!\d) (中国邮政编码为6位数字)
121+
122+
24 IP地址:\d+\.\d+\.\d+\.\d+ (提取IP地址时有用)
123+
124+
25 IP地址:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))
125+
</body>
126+
</html>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 路径问题
2+
## 1 "/" 的区别
3+
### 1.1 服务器端和客户浏览器端
4+
服务器 / 表示在webRoot的根目录下(不需要带项目名)
5+
浏览器 / 表示在webapps的根目录下(需要写项目名)
6+
### 1.2 应用实例
7+
转发
8+
~~~
9+
request.getRequestDispatcher("/target.html").forward(request, response);
10+
~~~
11+
请求重定向
12+
~~~
13+
response.sendRedirect("/project/target.html");
14+
~~~
15+
html页面超连接
16+
~~~
17+
response.getWriter().write("<html><body><a href='/project/target.html'>超链接</a></body></html>");
18+
~~~
19+
html页面中的form提交地址
20+
~~~
21+
<form action='/project/target.html'>
22+
~~~
23+
## 2 服务器端相对路径读取文件
24+
### 2.1 "." 定位到tomcat/bin目录下
25+
~~~
26+
//错误示例
27+
File("./src/db.properties");
28+
~~~
29+
### 2.2 web应用下加载资源文件的方法
30+
方法一:getRealPath() 读取,返回资源文件的绝对路径
31+
~~~
32+
String path = this.getServletContext().getRealPath("/WEB-INF/db.properties");
33+
File file = new File(path);
34+
FileInputStream in = new FileInputStream(file);
35+
~~~
36+
方法二:getResourceAsStream() 得到资源文件,返回输入流
37+
~~~
38+
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
39+
//读取资源文件
40+
Properties prop = new Properties();
41+
prop.load(in);
42+
String user = prop.getProperty("user");
43+
String password = prop.getProperty("password");
44+
~~~
45+
## 3 操作系统区别
46+
### 3.1 共同点
47+
>绝对路径:该文件在硬盘上的完整路径,一般都是以盘符开头的。
48+
相对路径:相对路径就是资源文件相对于当前程序所在的路径。
49+
. 当前路径
50+
.. 上一级路径
51+
52+
### 3.2 目录分隔符区别
53+
在windows机器上的目录分隔符是 \,在linux机器上的目录分隔符是 /
54+
,在windows上 \ 与 / 都可以使用作为目录分隔符,而且如果写 / 的时候只需要写一个即可
55+
路径示例:
56+
~~~
57+
----linux----
58+
/home/sunshine/data.txt
59+
./ 当前目录
60+
../ 上级目录
61+
----windows----
62+
程序中"\"需要写成"\\"
63+
E:\sunshine\a.txt
64+
E:/sunshine/a.txt
65+
E:
66+
../../
67+
~~~
68+
~~~
69+
System.out.println("目录分隔符:"+ File.separator);
70+
System.out.println("当前路径是:"+ file4.getAbsolutePath());//D:\Workspaces\java_test\.
71+
File parentFile = new File("E:\\");
72+
File file1 = new File("E:"+File.separator+"a.txt");
73+
File file2 = new File("E:/a.txt");
74+
File file3 = new File("E:\\","a.txt");
75+
File file4 = new File(".");
76+
File file5 = new File("..\\..\\a.txt");
77+
~~~
78+
## 4 URI、URL
79+
>URI:Uniform Resource Identifier,统一资源标识符
80+
URL:Uniform Resource Locator,统一资源定位符
81+
82+
URL是URI的一个特例,URL能够唯一定位资源
83+
【URI示例】
84+
85+
【URL示例】
86+
file:///E:/sunshine/a.txt
87+
http://www.csxiaoyao.com/
88+
ftp://home.ustc.edu.cn

0 commit comments

Comments
 (0)