You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by kr...@apache.org on 2022/01/07 21:42:03 UTC

[tinkerpop] 01/15: Initial changes for regex support. More to follow

This is an automated email from the ASF dual-hosted git repository.

krlawrence pushed a commit to branch TINKERPOP-2652
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit eeb584a642ae7db7b9e95029890a45ae71dcf5d5
Author: Kelvin Lawrence <gf...@yahoo.com>
AuthorDate: Thu Nov 18 13:47:40 2021 -0600

    Initial changes for regex support. More to follow
---
 .../tinkerpop/gremlin/process/traversal/Text.java  | 44 ++++++++++++++++++++++
 .../tinkerpop/gremlin/process/traversal/TextP.java | 18 +++++++++
 2 files changed, 62 insertions(+)

diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Text.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Text.java
index 9c25825..5d443ff 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Text.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/Text.java
@@ -19,6 +19,8 @@
 package org.apache.tinkerpop.gremlin.process.traversal;
 
 import java.util.function.BiPredicate;
+import java.util.regex.Pattern; 
+import java.util.regex.Matcher; 
 
 /**
  * {@link Text} is a {@link java.util.function.BiPredicate} that determines whether the first string starts with, starts
@@ -30,6 +32,48 @@ import java.util.function.BiPredicate;
 public enum Text implements BiPredicate<String, String> {
 
     /**
+     * Evaluates if the first string has a regex match with the second (pattern).
+     *
+     * @since 3.6.0
+     */
+    regex {
+        @Override
+        public boolean test(final String value, final String regex) {
+            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
+            Matcher matcher = pattern.matcher(value);
+            return matcher.find();   
+        }
+
+        /**
+         * The negative of {@code regex} is {@link #notRegex}.
+         */
+        @Override
+        public Text negate() {
+            return notRegex;
+        }
+    },
+    /**
+     * Evaluates if the first string does not have a regex match with the second (pattern).
+     *
+     * @since 3.6.0
+     */
+    notRegex {
+        @Override
+        public boolean test(final String value, final String regex) {
+	    Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
+	    Matcher matcher = pattern.matcher(value);
+	    return !matcher.find();   
+        }
+
+        /**
+         * The negative of {@code notRegex} is {@link #regex}.
+         */
+        @Override
+        public Text negate() {
+            return regex;
+        }
+    },
+    /**
      * Evaluates if the first string starts with the second.
      *
      * @since 3.4.0
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TextP.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TextP.java
index 2c28853..5b72521 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TextP.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/TextP.java
@@ -106,4 +106,22 @@ public class TextP extends P<String> {
     public static TextP notContaining(final String value) {
         return new TextP(Text.notContaining, value);
     }
+    
+    /**           
+     * Determines if String has a match with the given REGEX pattern. 
+     *
+     * @since 3.6.0
+     */
+    public static TextP regex(final String value) {
+        return new TextP(Text.regex, value);
+    }
+
+    /**           
+     * Determines if String has no match with the given REGEX pattern. 
+     *
+     * @since 3.6.0
+     */
+    public static TextP notRegex(final String value) {
+        return new TextP(Text.notRegex, value);
+    }
 }