Skip to content

Commit

Permalink
[FLINK-13237][table-planner-blink] Fix regexpReplace and regexpExtrac…
Browse files Browse the repository at this point in the history
…t to same of flink-planner
  • Loading branch information
JingsongLi authored and wuchong committed Aug 5, 2019
1 parent 302fc1d commit bab2249
Showing 1 changed file with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,15 @@ public static String splitIndex(String str, int character, int index) {
}

/**
* Returns a string resulting from replacing all substrings that match the regular
* expression with replacement.
* Returns a string resulting from replacing all substrings
* that match the regular expression with replacement.
*/
public static String regexpReplace(String str, String regex, String replacement) {
if (regex.isEmpty()) {
return str;
if (str == null || regex == null || replacement == null) {
return null;
}
try {
// we should use StringBuffer here because Matcher only accept it
StringBuffer sb = new StringBuffer();
Matcher m = REGEXP_PATTERN_CACHE.get(regex).matcher(str);
while (m.find()) {
m.appendReplacement(sb, replacement);
}
m.appendTail(sb);
return sb.toString();
return str.replaceAll(regex, Matcher.quoteReplacement(replacement));
} catch (Exception e) {
LOG.error(
String.format("Exception in regexpReplace('%s', '%s', '%s')", str, regex, replacement),
Expand All @@ -368,27 +361,26 @@ public static String regexpReplace(String str, String regex, String replacement)
}

/**
* Returns a string extracted with a specified regular expression and a regex
* match group index.
* Returns a string extracted with a specified regular expression and a regex match group index.
*/
public static String regexpExtract(String str, String regex, int extractIndex) {
if (extractIndex < 0) {
if (str == null || regex == null) {
return null;
}

try {
Matcher m = REGEXP_PATTERN_CACHE.get(regex).matcher(str);
Matcher m = Pattern.compile(regex).matcher(str);
if (m.find()) {
MatchResult mr = m.toMatchResult();
return mr.group(extractIndex);
}
return null;
} catch (Exception e) {
LOG.error(
String.format("Exception in regexpExtract('%s', '%s', '%d')", str, regex, extractIndex),
e);
return null;
}

return null;
}

public static String regexpExtract(String str, String regex, long extractIndex) {
Expand Down

0 comments on commit bab2249

Please sign in to comment.