-
Notifications
You must be signed in to change notification settings - Fork 22
/
RegexUtils.java
262 lines (239 loc) · 8.72 KB
/
RegexUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package run.ikaros.api.infra.utils;
import jakarta.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import run.ikaros.api.infra.exception.RegexMatchingException;
public class RegexUtils {
private static final Logger log = LoggerFactory.getLogger(RegexUtils.class);
/**
* Get file Postfix.
*/
@Nonnull
public static String getFilePostfix(@Nonnull String fileName) {
AssertUtils.notBlank(fileName, "fileName");
Set<String> strSet = new HashSet<>();
Matcher tagMatcher = Pattern.compile(RegexConst.FILE_POSTFIX).matcher(fileName);
while (tagMatcher.find()) {
strSet.add(tagMatcher.group());
}
return strSet.stream()
.findFirst()
.orElseThrow(() -> new RegexMatchingException("file postfix matching exception"));
}
/**
* Get file tag.
*/
@Nonnull
public static List<String> getFileTag(@Nonnull String fileName) {
AssertUtils.notBlank(fileName, "fileName");
if ("[]".equalsIgnoreCase(fileName)) {
return List.of();
}
List<String> stringList = new ArrayList<>();
Matcher tagMatcher = Pattern.compile(RegexConst.FILE_NAME_TAG).matcher(fileName);
while (tagMatcher.find()) {
stringList.add(tagMatcher.group());
}
return stringList.stream()
.map(postfix -> postfix.replace("[", "")
.replace("]", ""))
.filter(tag -> {
String seqStr = String.valueOf(getFileNameTagEpSeq(fileName));
if (seqStr.length() == 1) {
seqStr = "0" + seqStr;
}
return !tag.equalsIgnoreCase(seqStr);
})
.collect(Collectors.toList());
}
/**
* Get file name tag episode seq, such as: xxxxxxx[02]xxxxxx.mp4 => 02
* .
*/
@Nonnull
public static Long getFileNameTagEpSeq(@Nonnull final String fileName) {
AssertUtils.notBlank(fileName, "fileName");
Set<String> strSet = new HashSet<>();
if ("[]".equalsIgnoreCase(fileName)) {
return -1L;
}
String originalFileName = fileName;
// matching file tag that is seq
Matcher matcher =
Pattern.compile(RegexConst.FILE_NAME_TAG_EPISODE_SEQUENCE_WITH_BRACKETS)
.matcher(fileName);
while (matcher.find()) {
strSet.add(matcher.group());
}
return strSet.stream()
.map(postfix -> postfix.replace("[", "")
.replace("]", ""))
.flatMap((Function<String, Stream<Long>>) s -> {
try {
return Stream.of(Long.parseLong(s));
} catch (NumberFormatException numberFormatException) {
throw new RegexMatchingException(
"file name tag episode seq matching exception , file name: "
+ fileName);
}
}).findFirst().orElseThrow(() -> new RegexMatchingException(
"file name tag episode seq matching exception, file name: " + fileName));
}
/**
* Get episode seq by file name, such as: xxxx 04 xxxx.mp4 => 04 .
*/
@Nonnull
public static Long getFileNameBlankEpSeq(@Nonnull final String fileName) {
AssertUtils.notBlank(fileName, "fileName");
Set<String> strSet = new HashSet<>();
Matcher matcher =
Pattern.compile(RegexConst.FILE_NAME_EPISODE_SEQUENCE_WITH_BLANK)
.matcher(fileName);
while (matcher.find()) {
strSet.add(matcher.group());
}
if (strSet.isEmpty()) {
throw new RegexMatchingException(
"file name episode seq matching exception , file name: "
+ fileName);
}
return strSet.stream().findFirst()
.map(String::trim)
.map(Long::parseLong)
.orElse(null);
}
/**
* Get episode seq by file name and ep integrally, such as: xxxx EP04 xxxx.mp4 => 04 .
*/
@Nonnull
public static Long getEpFileNameIntegrallySeq(@Nonnull final String fileName) {
AssertUtils.notBlank(fileName, "fileName");
Set<String> strSet = new HashSet<>();
Matcher matcher =
Pattern.compile(RegexConst.FILE_NAME_EPISODE_SEQUENCE_WITH_INTEGRALLY_AND_BLANK)
.matcher(fileName);
while (matcher.find()) {
strSet.add(matcher.group());
}
if (strSet.isEmpty()) {
throw new RegexMatchingException(
"file name episode seq matching exception , file name: "
+ fileName);
}
return strSet.stream().findFirst()
.map(String::trim)
.map(str -> str.replace("EP", ""))
.map(Long::parseLong)
.orElse(null);
}
/**
* Get matching str by regex.
*/
@Nonnull
public static String getMatchingStr(@Nonnull String originalStr, @Nonnull String regex) {
AssertUtils.notBlank(originalStr, "originalStr");
AssertUtils.notBlank(regex, "regex");
List<String> strList = new ArrayList<>();
Matcher tagMatcher =
Pattern.compile(regex).matcher(originalStr);
while (tagMatcher.find()) {
strList.add(tagMatcher.group());
}
if (strList.isEmpty()) {
throw new RegexMatchingException(
"[" + regex + "] match fail for originalStr=" + originalStr);
}
StringBuilder sb = new StringBuilder();
for (String str : strList) {
sb.append(str);
}
return sb.toString().trim();
}
/**
* Get matching english str.
*/
@Nonnull
public static String getMatchingEnglishStr(String str) {
AssertUtils.notBlank(str, "str");
final String regex = "[A-Za-z\\s]";
return getMatchingStr(str, regex);
}
/**
* Get matching english str without tag.
*/
@Nonnull
public static String getMatchingEnglishStrWithoutTag(String str) {
AssertUtils.notBlank(str, "str");
final String originalStr = str;
str = str.replaceAll(RegexConst.FILE_NAME_TAG, "");
str = str.replaceAll(RegexConst.FILE_POSTFIX, "");
if (StringUtils.isBlank(str) || "[]".equalsIgnoreCase(str)) {
log.warn("str is blank after remove file tag, originalStr={}", originalStr);
// 针对全用中括号包裹的文件名称 获取第二个中括号的内容为标题
List<String> fileTagList = getFileTag(originalStr);
if (fileTagList.isEmpty() || fileTagList.size() <= 1) {
return str;
}
return fileTagList.get(1);
}
final String regex = "[A-Za-z\\s]";
return getMatchingStr(str, regex);
}
/**
* Get matching chinese str.
*/
@Nonnull
public static String getMatchingChineseStr(String str) {
AssertUtils.notBlank(str, "str");
final String regex = "[\\u2E80-\\u9FFF]";
return getMatchingStr(str, regex);
}
/**
* Get matching chinese str without tag.
*/
@Nonnull
public static String getMatchingChineseStrWithoutTag(String str) {
AssertUtils.notBlank(str, "str");
final String originalStr = str;
str = str.replaceAll(RegexConst.FILE_NAME_TAG, "");
str = str.replaceAll(RegexConst.FILE_POSTFIX, "");
str = str.replaceAll(RegexConst.FILE_NAME_TAG_EPISODE_SEQUENCE, "");
if (StringUtils.isBlank(str)) {
log.warn("str is blank after remove file tag, originalStr={}", originalStr);
return str;
}
final String regex = "[\\u2E80-\\u9FFF]";
return getMatchingStr(str, regex);
}
/**
* Parse episode seq by file name.
*/
@Nonnull
public static Long parseEpisodeSeqByFileName(String fileName) {
AssertUtils.notBlank(fileName, "fileName");
Long seq = -1L;
try {
seq = getFileNameBlankEpSeq(fileName);
} catch (RegexMatchingException e) {
try {
seq = getFileNameTagEpSeq(fileName);
} catch (RegexMatchingException e1) {
try {
seq = getEpFileNameIntegrallySeq(fileName);
} catch (RegexMatchingException e3) {
log.warn("parse episode seq fail by file name: {}.", fileName, e3);
}
}
}
return seq;
}
}