Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.Function;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class RxReplace {
- public static String text(String subjectString, Pattern regex, Function<String, String> fn) {
- StringBuffer resultString = new StringBuffer();
- Matcher regexMatcher = regex.matcher(subjectString);
- while (regexMatcher.find()) {
- regexMatcher.appendReplacement(resultString, fn.apply(subjectString.substring(regexMatcher.start(), regexMatcher.end())));
- }
- regexMatcher.appendTail(resultString);
- return resultString.toString();
- }
- public static String group(String subjectString, Pattern regex, Function<String, String> fn) {
- StringBuffer resultString = new StringBuffer();
- Matcher regexMatcher = regex.matcher(subjectString);
- while (regexMatcher.find()) {
- regexMatcher.appendReplacement(resultString, fn.apply(regexMatcher.group(1)));
- }
- regexMatcher.appendTail(resultString);
- return resultString.toString();
- }
- }
Add Comment
Please, Sign In to add comment