public static String[] split(String string, String delimRegex, int group) { String delimPlaceholder = "<delimiter>"; string = RegexUtil.replaceAll(string, delimRegex, group, delimPlaceholder); return ListUtil.stringAsArray(string, delimPlaceholder); }
public static String replaceAll(String string, String regex, int group, String replacement) { return replaceAll(string, getPattern(regex), group, replacement); }
private String[] extractFields(String tableTemplate) { List<String> list = new LinkedList<String>(); String st = tableTemplate; while (true) { Matcher matcher = fieldPattern.matcher(st); if (matcher.find()) { int start = matcher.start(0); int end = matcher.end(0); int start2 = matcher.start(1); int end2 = matcher.end(1); String field = st.substring(start, end); String fieldName = st.substring(start2, end2); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, ""); list.add(fieldName); } else break; } return ListUtil.stringListAsArray(list); }
private void registerFields(String st, boolean isMultiple) { while (true) { Matcher matcher = fieldPattern.matcher(st); if (matcher.find()) { int start = matcher.start(0); int end = matcher.end(0); int start2 = matcher.start(1); int end2 = matcher.end(1); String field = st.substring(start, end); String fieldName = st.substring(start2, end2); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, ""); registerField(fieldName, isMultiple); } else break; } }
private void collectSimpleFieldLeads() { String st = template; while (true) { Matcher matcher = fieldLeadPattern.matcher(st); if (matcher.find()) { int start = matcher.start(0); int end = matcher.end(0); int start1 = matcher.start(1); int end1 = matcher.end(1); int start2 = matcher.start(2); int end2 = matcher.end(2); String field = st.substring(start, end); String fieldLead = st.substring(start1, end1); String fieldName = st.substring(start2, end2); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, ""); if (simpleFields.containsKey(fieldName)) { simpleFieldLeads.put(fieldName, fieldLead); } } else break; } }
private String fillRow(String tableTemplate, int rowIndex) { String st = tableTemplate; Matcher m = wsPattern.matcher(st); String leadingWhitespace = null; if (m.find()) { // Find the 1-st whitespace leadingWhitespace = m.group(); } while (true) { Matcher matcher = fieldPattern.matcher(st); if (matcher.find()) { int start = matcher.start(0); int end = matcher.end(0); int start2 = matcher.start(1); int end2 = matcher.end(1); String field = st.substring(start, end); String fieldName = st.substring(start2, end2); List<String> column = multiFields.get(fieldName); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, column.get(rowIndex)); st = st.replaceAll("\n", "\n" + leadingWhitespace); } else break; } return st; }
private void registerFields() { String st = template; while (true) { Matcher matcher = tablePattern.matcher(st); if (matcher.find()) { int start = matcher.start(0); int end = matcher.end(0); int start1 = matcher.start(1); int end1 = matcher.end(1); int start2 = matcher.start(2); int end2 = matcher.end(2); String tableField = st.substring(start, end); String table = st.substring(start1, end1) + st.substring(start2, end2); registerField(table, false); st = RegexUtil.replaceAll(st, RegexUtil.escape(tableField), 0, ""); // remove table not to get in the way of simple fields search registerFields(table, true); } else break; } registerFields(st, false); // register simple fields collectSimpleFieldLeads(); }
String value = getData(fieldName); if (! Util.isEmpty(value)) st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, value); else { st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, st.substring(start1, end1) + "{?" + st.substring(start2, end2) + "}"); if (!Util.isEmpty(value)) { value = value.replaceAll("\n", "\n" + simpleFieldLeads.get(fieldName)); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, value); st = RegexUtil.replaceAll(st, RegexUtil.escape(field), 0, "[?" + fieldName + "]");
trimmedChunk = RegexUtil.replaceAll(trimmedChunk, escapedQualifierPattern, 1, ESCAPED_QUALIFIER); trimmedChunk = RegexUtil.replaceAll(trimmedChunk, escapedQualifierPattern, 3, ESCAPED_QUALIFIER); numLeadingQ = trimmedChunk.startsWith(params.textQualifier) ? 1 : 0;
string = RegexUtil.replaceAll(string, "\\{.*?(,).*?\\}", 1, commaPlaceholder);