Package gnu.regexp

Examples of gnu.regexp.REMatch


    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    int count = 0;
    long atByte = 0;
    int atLine = 1;
    String line;
    REMatch match;
   
    try {
      while ((line = br.readLine()) != null) {
  match = pattern.getMatch(line);
  if (((options[LINE_REGEXP] && pattern.isMatch(line))
       || (!options[LINE_REGEXP] && (match != null)))
      ^ options[REVERT_MATCH]) {
    count++;
    if (!options[COUNT]) {
      if (options[QUIET]) {
        return true;
      }
      if (options[FILES_WITH_MATCHES]) {
        if (filename != null)
    out.println(filename);
        return true;
      }
      if (options[FILES_WITHOUT_MATCH]) {
        return false;
      }
      if (filename != null) {
        out.print(filename);
        out.print(':');
      }
      if (options[LINE_NUMBER]) {
        out.print(atLine);
        out.print(':');
      }
      if (options[BYTE_OFFSET]) {
        out.print(atByte + match.getStartIndex() );
        out.print(':');
      }
      out.println(line);
    }
  } // a match
View Full Code Here


      String delim;

      RE findDelim = new RE("[$%@].+[\\s]*=~[\\s]*[m]?(.)", 0,
          RESyntax.RE_SYNTAX_PERL5);

      REMatch match = findDelim.getMatch(line);
      if (match == null)
        return false;
      delim = match.toString(1);
      if (delim == null)
        return false;
      String temp = line;
      temp.replaceAll("\\" + delim, "xx");
      RE findRegExp = new RE("([$%@][^\\s]+)[\\s]*=~[\\s]*[m]?" + delim
          + "(.*)" + delim + "(.*)", 0, RESyntax.RE_SYNTAX_PERL5);
      match = findRegExp.getMatch(temp);
      if (match == null)
        return false;

      String var = line.substring(match.getStartIndex(1), match
          .getEndIndex(1));
      String text = line.substring(match.getStartIndex(2), match
          .getEndIndex(2));
      String mod = line.substring(match.getStartIndex(3), match
          .getEndIndex(3));
      if (var == null || text == null)
        return false;

      if (mod != null && mod.indexOf("i") >= 0)
View Full Code Here

        // mRe_IP_Pos handles locations like
        //     main::(/some/path/foobar.pl:7):
        // mRe_IP_Pos_Eval handles locations like
        //

        REMatch result = re.IP_POS_CODE.getMatch(output);
        if (result == null)
        {
            result = re.IP_POS.getMatch(output);
            if (result == null)
                throw new IOException("could not match re.IP_POS in {" + output + "}");
            String filename = result.toString(1);
            REMatch temp = re.IP_POS_EVAL.getMatch(filename);
            if (temp != null) result = temp;
        }

        return new IPPosition(
            new Path(result.toString(1)),
View Full Code Here

      }

      boolean containProtocol = false;
      try {
        final RE re = new RE(SPConstants.URL_SEP);
        final REMatch reMatch = re.getMatch(patternDecoded);
        if (reMatch != null) {
          containProtocol = true; // protocol is present
        }

      } catch (final Exception e) {
View Full Code Here

      final String strContainKey = new String(
          tempBuffer.delete(0, SPConstants.CONTAINS.length()));
      RE re;
      try {
        re = new RE(strContainKey); // with case
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexp
    // if pattern starts with "regexp:", then check for regex match with
    // case
    if (pattern.startsWith(SPConstants.REGEXP)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexPattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP.length()));
      RE re;
      try {
        re = new RE(strRegexPattrn);
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexpCase
    // if pattern starts with "regexpCase:", then check for regex match with
    // case
    if (pattern.startsWith(SPConstants.REGEXP_CASE)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexCasePattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP_CASE.length()));
      RE re;
      try {
        re = new RE(strRegexCasePattrn);
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle regexpIgnoreCase
    // if pattern starts with "regexpIgnoreCase:", then check for regex
    // match without case
    if (pattern.startsWith(SPConstants.REGEXP_IGNORE_CASE)) {
      final StringBuffer tempBuffer = new StringBuffer(pattern);
      final String strRegexIgnoreCasePattrn = new String(
          tempBuffer.delete(0, SPConstants.REGEXP_IGNORE_CASE.length()));
      RE re;
      try {
        re = new RE(strRegexIgnoreCasePattrn, RE.REG_ICASE); // ignore
        // case
        final REMatch reMatch = re.getMatch(strValue);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // handle "^" and "$"
    if (pattern.startsWith(SPConstants.CARET)
        || pattern.endsWith(SPConstants.DOLLAR)) {
      StringBuffer tempBuffer = new StringBuffer(pattern);
      boolean bDollar = false;
      String strValueModified = strValue;
      if (pattern.startsWith(SPConstants.CARET)) {
        URL urlValue;
        try {
          urlValue = new URL(strValue);
          int port = urlValue.getPort();
          if (port == -1) {
            port = urlValue.getDefaultPort();
            strValueModified = urlValue.getProtocol() + SPConstants.URL_SEP
                + urlValue.getHost() + SPConstants.COLON + port
                + urlValue.getFile();
          }
        } catch (final MalformedURLException e1) {
          LOGGER.log(Level.FINE, e1.getMessage());
          return false;
        }
        tempBuffer = new StringBuffer(pattern);
        final int indexOfStar = tempBuffer.indexOf("*");
        if (indexOfStar != -1) {
          tempBuffer.replace(indexOfStar, indexOfStar + "*".length(), "[0-9].*");
        } else {
          tempBuffer.delete(0, "^".length());
          if (pattern.endsWith(SPConstants.DOLLAR)) {
            bDollar = true;
            tempBuffer.delete(tempBuffer.length() - SPConstants.DOLLAR.length(), tempBuffer.length());
          }
          try {
            final URL urlPatt = new URL(tempBuffer.toString());
            final int port = urlPatt.getPort();

            final String strHost = urlPatt.getHost().toString();

            if ((port == -1) && (strHost != null) && (strHost.length() != 0)) {
              tempBuffer = new StringBuffer("^" + urlPatt.getProtocol()
                  + SPConstants.URL_SEP + urlPatt.getHost() + ":[0-9].*"
                  + urlPatt.getPath());
            }
            if (bDollar) {
              tempBuffer.append(SPConstants.DOLLAR);
            }
          } catch (final MalformedURLException e) {
            LOGGER.log(Level.FINE, e.getMessage());
            tempBuffer = new StringBuffer(pattern);
          }
        }
      }

      RE re;
      try {
        re = new RE(tempBuffer);
        final REMatch reMatch = re.getMatch(strValueModified);
        if (reMatch != null) {
          return true;
        }
        return false;
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    }

    // url decode the pattern
    String patternDecoded = pattern;
    try {
      patternDecoded = URLDecoder.decode(pattern, "UTF-8");
    } catch (final Exception e) {
      LOGGER.log(Level.FINE, e.getMessage());
      patternDecoded = pattern;
    }

    if (patternDecoded == null) {
      return false;
    }

    boolean containProtocol = false;
    try {
      final RE re = new RE(SPConstants.URL_SEP);
      final REMatch reMatch = re.getMatch(patternDecoded);
      if (reMatch != null) {
        containProtocol = true; // protocol is present
      }
    } catch (final REException e) {
      containProtocol = false;
    }

    if (containProtocol) {
      // split the test URL into two parts
      String urlValue1stPart = null;
      String urlValue2ndPart = null;

      URL urlValue;
      try {
        urlValue = new URL(strValue);
        int port = urlValue.getPort();
        if (port == -1) {
          port = urlValue.getDefaultPort();
        }
        urlValue1stPart = urlValue.getProtocol() + SPConstants.URL_SEP
            + urlValue.getHost() + SPConstants.COLON + port;
        urlValue2ndPart = urlValue.getFile();

        if (urlValue2ndPart != null) {
          if (!urlValue2ndPart.startsWith(SPConstants.SLASH)) {
            urlValue2ndPart = SPConstants.SLASH + urlValue2ndPart;
          }
        }
      } catch (final MalformedURLException e1) {
        LOGGER.log(Level.FINE, e1.getMessage());
        return false;
      }

      // split the pattern into two parts
      String urlPatt1stPart = null;
      String urlPatt2ndPart = null;
      boolean bPortStar = false;
      try {
        final URL urlPatt = new URL(patternDecoded);
        final int port = urlPatt.getPort();
        String strPort = "";
        if (port == -1) {
          strPort = "[0-9].*";
        } else {
          strPort = port + "";
        }
        urlPatt1stPart = "^" + urlPatt.getProtocol() + SPConstants.URL_SEP
            + urlPatt.getHost() + SPConstants.COLON + strPort;
        if (!(urlPatt.getFile()).startsWith(SPConstants.SLASH)) { // The
          // pattern
          // must
          // have
          // "/"
          // at
          // after
          // the
          // port
          return false;
        }
        urlPatt2ndPart = "^" + urlPatt.getFile();
      } catch (final MalformedURLException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        bPortStar = true;
      }

      if (bPortStar) {
        final int indexOfStar = patternDecoded.indexOf("*");
        if (indexOfStar != -1) {
          urlPatt1stPart = "^" + patternDecoded.substring(0, indexOfStar)
              + "[0-9].*";
          if (!(patternDecoded.substring(indexOfStar + 1)).startsWith(SPConstants.SLASH)) {
            return false;
          }
          urlPatt2ndPart = "^" + patternDecoded.substring(indexOfStar + 1);
        }
      }

      // check 1st part of both with ignorecase
      RE re;
      try {
        re = new RE(urlPatt1stPart, RE.REG_ICASE); // ignore case for
        // 1st part
        REMatch reMatch = re.getMatch(urlValue1stPart);
        if (reMatch != null) {
          // check 2nd part of both with case
          re = new RE(urlPatt2ndPart);
          reMatch = re.getMatch(urlValue2ndPart);
          if (reMatch != null) {
            return true;
          }
        }
      } catch (final REException e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      } catch (final Exception e) {
        LOGGER.log(Level.FINE, e.getMessage());
        return false;
      }
    } else {
      String pat1 = null;
      String pat2 = null;
      // split the pattern into two parts
      if (patternDecoded.indexOf(SPConstants.SLASH) != -1) {
        if (patternDecoded.indexOf(SPConstants.COLON) == -1) {
          pat1 = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH))
              + ":[0-9].*";
        } else {
          pat1 = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH));
        }
        pat2 = patternDecoded.substring(patternDecoded.indexOf(SPConstants.SLASH));
      } else {
        // The pattern must have "/" at after the port
        return false;
      }

      pat1 = "^.*://.*" + pat1;
      pat2 = "^" + pat2;
      URL urlValue;
      try {
        urlValue = new URL(strValue);
        int port = urlValue.getPort();
        if (port == -1) {
          port = urlValue.getDefaultPort();
        }
        final String urlValue1stPart = urlValue.getProtocol()
            + SPConstants.URL_SEP + urlValue.getHost() + SPConstants.COLON
            + port;
        String urlValue2ndPart = urlValue.getFile();

        if (urlValue2ndPart != null) {
          if (!urlValue2ndPart.startsWith(SPConstants.SLASH)) {
            urlValue2ndPart = SPConstants.SLASH + urlValue2ndPart;
          }
        }

        RE re;
        try {
          re = new RE(pat1, RE.REG_ICASE); // ignore case for 1st part
          REMatch reMatch = re.getMatch(urlValue1stPart);
          if (reMatch != null) {
            re = new RE(pat2); // with case for 2nd part
            reMatch = re.getMatch(urlValue2ndPart);
            if (reMatch != null) {
              return true;
View Full Code Here

TOP

Related Classes of gnu.regexp.REMatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.