Contains support now regex as second argument

This commit is contained in:
Maxime Gobert 2018-03-28 17:25:11 +02:00
parent 14e49d0099
commit a410639627

View File

@ -34,6 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions.strings; package com.google.refine.expr.functions.strings;
import java.util.Properties; import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
@ -49,6 +51,14 @@ public class Contains implements Function {
Object s2 = args[1]; Object s2 = args[1];
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
return ((String) s1).indexOf((String) s2) > -1; return ((String) s1).indexOf((String) s2) > -1;
} else if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof Pattern){
String s = (String) s1;
Pattern pattern = (Pattern) s2;
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return true;
}else
return false;
} }
} }
return null; return null;