-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRegexThis.java
More file actions
51 lines (48 loc) · 2.01 KB
/
RegexThis.java
File metadata and controls
51 lines (48 loc) · 2.01 KB
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
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexThis {
//3 to 5 lines of regex
public static List<Item> regexer(String raw){
Pattern p = Pattern.compile("(?:name\\W)?(?<NAME>[\\w]+)?(?:\\Wprice\\W)?(?<PRICE>[\\w\\.]+)?(?:\\Wtype\\W)?(?<TYPE>[\\w]+)?(?:\\Wexpiration\\W)?(?<EXPIRATION>[\\w\\/]+)?(?:##)"
,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(raw);
//matches gotten
List<Item> items = new ArrayList<Item>();
//encapsulate entries
while(m.find()){
Item newbie = new Item.ItemBuilder()
.setName(m.group("NAME") != null ? (Pattern.compile("0")).matcher(m.group("NAME")).replaceAll("o") : null)
.setPrice(m.group("PRICE"))
.setType(m.group("TYPE"))
.setExpiration(m.group("EXPIRATION"))
.build();
items.add(newbie);
}
return items;
}
public static List<Item> beautifier(List<Item> raw){
for(Item item: raw){
String a = item.getName();
if(a != null) {
Matcher cookie = Pattern.compile("cookies", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher milk = Pattern.compile("milk", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher bread = Pattern.compile("bread", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher apples = Pattern.compile("apples", Pattern.CASE_INSENSITIVE).matcher(a);
if (cookie.matches()) {
item.setName("COOKIES");
} else if (milk.matches()) {
item.setName("MILK");
} else if (bread.matches()) {
item.setName("BREAD");
} else if (apples.matches()) {
item.setName("APPLES");
}
}
}
return raw;
}
}
//TODO:
//nicely formatted summary(kind of)
//replace inconsistent casing with regular casing