<commit_before>package com.github.rafasantos.formarter;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.github.rafasantos.pojo.LineDelimitedPojo;
import com.github.rafasantos.util.AppConstants;

public class LinePojoFormatter {
	

	public static String formatTemplate(String template, LineDelimitedPojo p) {
		Set<String> matches = findMatches(template);
		for (String s : matches) {
			if (AppConstants.ORIGINAL_LINE.equals(s)) {
				template = template.replaceAll("\\{" + s + "\\}", p.getOriginalLine());
			} else {
				String value = "";
				try {
					Integer key = new Integer(s);
					String valueCandidate = p.getField(key);
					if (valueCandidate != null) {
						value = valueCandidate;
					}
				} catch (Exception e) {
					// TODO: log exception
				}
				template = template.replaceAll("\\{" + s + "\\}", value);
			}
		}
		return template;
	}

	private static Set<String> findMatches(String template) {
		Set<String> result = new HashSet<>();
		if (template != null) {
			String s = template;
			Pattern p = Pattern.compile("\\{\\d+\\}|"+AppConstants.ORIGINAL_LINE);
			Matcher m = p.matcher(template);
			while (m.find()) {
				if (AppConstants.ORIGINAL_LINE.equals(s.substring(m.start(), m.end()))) {
					result.add(s.substring(m.start(), m.end()));
				} else {
					int startMatcher = m.start() + 1;
					int endMatcher = m.end() - 1;
					result.add(s.substring(startMatcher, endMatcher));
				}
			}
		}
		return result;
	}

}
<commit_msg>Fixed bug when replacing {ORIGINAL_LINE} templates on lines containig regular expression characters.<commit_after>package com.github.rafasantos.formarter;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.github.rafasantos.pojo.LineDelimitedPojo;
import com.github.rafasantos.util.AppConstants;

public class LinePojoFormatter {
	

	public static String formatTemplate(String template, LineDelimitedPojo p) {
		Set<String> matches = findMatches(template);
		for (String s : matches) {
			if (AppConstants.ORIGINAL_LINE.equals(s)) {
				template = template.replace("{" + s + "}", p.getOriginalLine());
			} else {
				String value = "";
				try {
					Integer key = new Integer(s);
					String valueCandidate = p.getField(key);
					if (valueCandidate != null) {
						value = valueCandidate;
					}
				} catch (Exception e) {
					// TODO: log exception
				}
				template = template.replaceAll("\\{" + s + "\\}", value);
			}
		}
		return template;
	}

	private static Set<String> findMatches(String template) {
		Set<String> result = new HashSet<>();
		if (template != null) {
			String s = template;
			Pattern p = Pattern.compile("\\{\\d+\\}|"+AppConstants.ORIGINAL_LINE);
			Matcher m = p.matcher(template);
			while (m.find()) {
				if (AppConstants.ORIGINAL_LINE.equals(s.substring(m.start(), m.end()))) {
					result.add(s.substring(m.start(), m.end()));
				} else {
					int startMatcher = m.start() + 1;
					int endMatcher = m.end() - 1;
					result.add(s.substring(startMatcher, endMatcher));
				}
			}
		}
		return result;
	}

}

{'fraction_non_alphanumeric': 0.14968354430379746, 'fraction_numerical': 0.0012658227848101266, 'mean_word_length': 3.36, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6615444', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1035, 'n_words': 223}