metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ottobackwards <...@git.apache.org>
Subject [GitHub] metron pull request #638: METRON-933 New stellar function for regex group ca...
Date Tue, 11 Jul 2017 18:57:14 GMT
Github user ottobackwards commented on a diff in the pull request:

    https://github.com/apache/metron/pull/638#discussion_r126778367
  
    --- Diff: metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/RegExFunctions.java
---
    @@ -0,0 +1,103 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.metron.stellar.dsl.functions;
    +
    +import java.util.List;
    +import java.util.regex.Matcher;
    +import java.util.regex.Pattern;
    +import org.apache.metron.stellar.common.utils.ConversionUtils;
    +import org.apache.metron.stellar.common.utils.PatternCache;
    +import org.apache.metron.stellar.dsl.BaseStellarFunction;
    +import org.apache.metron.stellar.dsl.Stellar;
    +
    +public class RegExFunctions {
    +
    +  @Stellar(name = "REGEXP_MATCH",
    +      description = "Determines whether a regex matches a string",
    +      params = {
    +          "string - The string to test",
    +          "pattern - The proposed regex pattern"
    +      },
    +      returns = "True if the regex pattern matches the string and false if otherwise.")
    +  public static class RegexpMatch extends BaseStellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> list) {
    +      if (list.size() < 2) {
    +        throw new IllegalStateException(
    +            "REGEXP_MATCH expects two args: [string, pattern] where pattern is a regexp
pattern");
    +      }
    +      String patternString = (String) list.get(1);
    +      String str = (String) list.get(0);
    +      if (str == null || patternString == null) {
    +        return false;
    +      }
    +      return PatternCache.INSTANCE.getPattern(patternString).matcher(str).matches();
    +    }
    +  }
    +
    +  @Stellar(name = "REGEXP_GROUP_VAL",
    +      description = "Returns the value of a group in a regex against a string",
    +      params = {
    +          "string - The string to test",
    +          "pattern - The proposed regex pattern",
    +          "group - integer that selects what group to select, starting at 1"
    +      },
    +      returns = "The value of the group, or null if not matched or no group at index")
    +  public static class RegexpGroupValue extends BaseStellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> list) {
    +      if (list.size() < 3) {
    --- End diff --
    
    Addressed, good catch


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message