Processing Large Files in Java, Variation 3
--- ReadFileJavaApplicationBufferedReader2.java | |
+++ ReadFileJavaApplicationBufferedReader3.java | |
@@ -44,6 +45,7 @@ | |
Instant commonNameStart = Instant.now(); | |
ArrayList<String> firstNames = new ArrayList<>(); | |
+ var namePat = Pattern.compile(", \\s*(([^ ]*), |([^ ]+))"); | |
System.out.println("Reading file using " + Caller.getName()); | |
@@ -55,20 +57,13 @@ | |
String name = array1[7].strip(); | |
names.add(name); | |
- if(name.contains(", ")) { | |
- | |
- String array2[] = (name.split(", ")); | |
- String firstHalfOfName = array2[1].trim(); | |
- | |
- if (!firstHalfOfName.isEmpty()) { | |
- if (firstHalfOfName.contains(" ")) { | |
- String array3[] = firstHalfOfName.split(" "); | |
- String firstName = array3[0].trim(); | |
- firstNames.add(firstName); | |
- } else { | |
- firstNames.add(firstHalfOfName); | |
- } | |
+ var matcher = namePat.matcher(name); | |
+ if (matcher.find()) { | |
+ String s = matcher.group(2); | |
+ if (s == null) { | |
+ s = matcher.group(3); | |
} | |
+ firstNames.add(s); | |
} | |
String rawDate = array1[4].strip(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment