Update Find and Tests

This commit is contained in:
Owen Stephens 2019-07-21 13:34:18 +01:00
parent d6999de0da
commit ac7b5a0a19
2 changed files with 12 additions and 3 deletions

View File

@ -46,7 +46,15 @@ public class Find implements Function {
Object s = args[0];
Object p = args[1];
if (s != null && p != null && (p instanceof String || p instanceof Pattern)) {
if (s != null && p != null && p instanceof String) {
int fromIndex = 0;
while ((fromIndex = s.toString().indexOf(p.toString(), fromIndex)) != -1 ){
allMatches.add(p.toString());
fromIndex++;
}
}
if (s != null && p != null && p instanceof Pattern) {
Pattern pattern = (p instanceof String) ? Pattern.compile((String) p) : (Pattern) p;

View File

@ -27,6 +27,7 @@
package com.google.refine.tests.expr.functions;
import java.util.Properties;
import java.util.regex.Pattern;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
@ -82,8 +83,8 @@ public class FindFunctionTests extends RefineTest {
}
@Test
public void findFunctionFindAllTest2() throws Exception {
String[] matches = (String[]) invoke("find", "hello 123456 goodbye.", "\\d{6}|hello");
public void findFunctionFindRegexTest() throws Exception {
String[] matches = (String[]) invoke("find", "hello 123456 goodbye.", Pattern.compile("\\d{6}|hello"));
Assert.assertEquals(matches[0], "hello");
Assert.assertEquals(matches[1], "123456");
}