コード例 #1
0
 /**
  * Return true if the regexp pattern is found to occur in the input string.
  *
  * @param input the input string
  * @param p the pattern
  */
 public static boolean regexp(String input, String p) {
   try {
     Pattern pattern = compiler.compile(p);
     return sMatcher.contains(input, pattern);
   } catch (MalformedPatternException e) {
     // We should probably print something to a debug log or something to mention that the pattern
     // we tested
     // threw an error and probably has some bogus regexp syntax.
     return false;
   }
 }
コード例 #2
0
  /**
   * Perform string substitution using pattern matching.
   *
   * @param str the source string
   * @param p pattern to look for
   * @param s the string to replace <i>pattern</i> with. Perl5 references to matches are allowed.
   *     See <a
   *     href="http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html">http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html</a>
   * @param numSubs number of substitutions to perform, Util.SUBSTITUTE_ALL will cause all
   *     occurences to be replaced
   * @return the string with the substitution made for numSubs occurences of the pattern
   */
  public static String regsub(String str, String p, String s, int numSubs) {
    try {
      Pattern pattern = compiler.compile(p);
      Perl5Substitution subst = new Perl5Substitution(s);

      String result = Util.substitute(sMatcher, pattern, subst, str, numSubs);
      return result;
    } catch (MalformedPatternException e) {
      throw new CompilationError(e);
    }
  }