Skip to content

Commit ff7228e

Browse files
author
chenweijie
committed
正则表达式1
1 parent 1649a6b commit ff7228e

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

src/main/java/com/chen/api/util/regex/RegexTest1.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.chen.api.util.regex;
22

3-
import jdk.internal.util.xml.impl.Input;
43
import org.junit.Test;
54

65
import java.util.regex.Matcher;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.chen.api.util.regex;
2+
3+
import org.junit.Test;
4+
5+
import javax.imageio.ImageIO;
6+
import java.awt.image.BufferedImage;
7+
import java.io.BufferedReader;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.net.URL;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
14+
15+
/**
16+
* Description:
17+
*
18+
* @author chenweijie
19+
*/
20+
public class RegexTest2 {
21+
22+
/**
23+
* 匹配图片地址中的src中的内容
24+
*/
25+
private static Pattern SRC_PATTERN = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)");
26+
27+
/**
28+
* 图片中url包含的?特殊字符
29+
*/
30+
private static String QUESTION_MARK = "?";
31+
32+
33+
@Test
34+
public void test1() {
35+
36+
Pattern pattern =Pattern.compile("//(//d{3}//)//s//d{3}-//d{4}");
37+
try {
38+
try (BufferedReader in = new BufferedReader(new FileReader("\testPatter.txt"))) {
39+
String s ;
40+
while ((s=in.readLine())!=null){
41+
42+
Matcher matcher =pattern.matcher(s);
43+
if (matcher.find()){
44+
System.out.println(matcher.group());
45+
}
46+
}
47+
}
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
54+
/**
55+
* 解析html数据 匹配上的返回处理后的数据,匹配不上的返回 null
56+
*
57+
* @param text html数据
58+
* @param id 主键id
59+
* @return 处理完的html数据
60+
*/
61+
private String resolveHtml(String text, Integer id) {
62+
63+
String img;
64+
Pattern pImage;
65+
Matcher mImage;
66+
//图片链接地址
67+
String regExImg = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
68+
pImage = Pattern.compile(regExImg, Pattern.CASE_INSENSITIVE);
69+
mImage = pImage.matcher(text);
70+
while (mImage.find()) {
71+
// 得到<img />数据
72+
img = mImage.group();
73+
// 匹配<img>中的src数据
74+
Matcher m = SRC_PATTERN.matcher(img);
75+
while (m.find()) {
76+
//取出图片的地址
77+
String imgUlr = m.group(1);
78+
//处理图片地址加上长宽参数
79+
String newUrl = replaceImg(imgUlr, id);
80+
//替换原内容中的标签
81+
if (newUrl != null) {
82+
text = text.replace(imgUlr, newUrl);
83+
} else {
84+
return null;
85+
}
86+
}
87+
88+
return text;
89+
}
90+
return null;
91+
}
92+
93+
94+
/**
95+
* 处理某一张图片的格式
96+
*
97+
* @param imageUrl 原图片的url
98+
* @param id 图片对应的id
99+
*/
100+
private String replaceImg(String imageUrl, Integer id) {
101+
102+
BufferedImage image = getBufferedImage(id, imageUrl);
103+
String mewImgURL = null;
104+
if (image != null) {
105+
int height = image.getHeight();
106+
int width = image.getWidth();
107+
108+
if (imageUrl.contains(QUESTION_MARK)) {
109+
mewImgURL = imageUrl + "&width=" + width + "&height=" + height;
110+
} else {
111+
mewImgURL = imageUrl + "?width=" + width + "&height=" + height;
112+
}
113+
}
114+
return mewImgURL;
115+
}
116+
117+
/**
118+
* 根据imgUrl获取BufferedImage对象
119+
*
120+
* @param id id
121+
* @param imgUrl 图片地址
122+
* @return BufferedImage对象
123+
*/
124+
private static BufferedImage getBufferedImage(Integer id, String imgUrl) {
125+
URL url;
126+
InputStream is = null;
127+
BufferedImage img = null;
128+
try {
129+
url = new URL(imgUrl);
130+
is = url.openStream();
131+
img = ImageIO.read(is);
132+
} catch (IOException e) {
133+
System.out.println("handleAnalysisPicture failed");
134+
} finally {
135+
try {
136+
if (is != null) {
137+
is.close();
138+
}
139+
} catch (IOException e) {
140+
e.printStackTrace();
141+
}
142+
}
143+
return img;
144+
}
145+
146+
147+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.chen.api.util.regex;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* 测试正则表达式类
8+
*
9+
* @author Chen WeiJie
10+
* @date 2017-11-16 21:31
11+
**/
12+
public class TestRegularExpression {
13+
14+
15+
//http://blog.csdn.net/allwefantasy/article/details/3136570/
16+
public static void main(String[] args) {
17+
18+
if (args.length < 2) {
19+
System.out.println("输入的字符串为空!");
20+
System.exit(0);
21+
}
22+
System.out.println("输入的字符串为: \"" + args[0] + "\"");
23+
for (int i = 1; i < args.length; i++) {
24+
System.out.println("正则表达式为: \"" + args[i] + "\"");
25+
Pattern p = Pattern.compile(args[i]);
26+
Matcher m = p.matcher(args[0]);
27+
while (m.find()) {
28+
System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1));
29+
}
30+
}
31+
}
32+
33+
34+
}

src/main/resources/testPatter.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(212) 555-1212
2+
(212) 543-1212
3+
(212) 458-1212
4+
(212) 545-1212

0 commit comments

Comments
 (0)