Package java.util.regex

Examples of java.util.regex.Matcher


     
      // display module-specific help
      if (arguments != null &&
        arguments.trim().length() > 0)
      {
        Matcher logsearch_matcher = HELP_PATTERN.matcher(arguments.toLowerCase());
       
        // check if the syntax is correct
        if (!logsearch_matcher.matches() ||
          (logsearch_matcher.groupCount() < 1 &&
           logsearch_matcher.groupCount() > 2))
        {
          bot.send(new Privmsg(nick, "Invalid syntax '"+command+" "+arguments+"'."));
          return;
        }
       
        // get the different help arguments
        String module_name = null;
        String help_key = null;
        module_name = logsearch_matcher.group(1);
        if (logsearch_matcher.groupCount() > 1)
        {
          help_key = logsearch_matcher.group(2);
          if (0 == help_key.length())
          {
            help_key = null;
          }
        }


        // osgiVersion = m.group(1) + "." + m.group(3);
        // }

        /* TODO need a regexp guru here */

        Matcher m;

        /* if it's already OSGi compliant don't touch it */
        m = OSGI_VERSION_PATTERN.matcher(version);
        if (m.matches()) {
            return version;
        }

        osgiVersion = version;

        /* check for dated snapshot versions with only major or major and minor */
        m = DATED_SNAPSHOT.matcher(osgiVersion);
        if (m.matches()) {
            String major = m.group(1);
            String minor = (m.group(3) != null) ? m.group(3) : "0";
            String service = (m.group(5) != null) ? m.group(5) : "0";
            String qualifier = m.group(6).replaceAll("-", "_").replaceAll("\\.", "_");
            osgiVersion = major + "." + minor + "." + service + "." + qualifier;
        }

        /* else transform first - to . and others to _ */
        osgiVersion = osgiVersion.replaceFirst("-", "\\.");
        osgiVersion = osgiVersion.replaceAll("-", "_");
        m = OSGI_VERSION_PATTERN.matcher(osgiVersion);
        if (m.matches()) {
            return osgiVersion;
        }

        /* remove dots in the middle of the qualifier */
        m = DOTS_IN_QUALIFIER.matcher(osgiVersion);
        if (m.matches()) {
            String s1 = m.group(1);
            String s2 = m.group(2);
            String s3 = m.group(3);
            String s4 = m.group(4);

            Matcher qualifierMatcher = ONLY_NUMBERS.matcher(s3);
            /*
             * if last portion before dot is only numbers then it's not in the middle of the
             * qualifier
             */
            if (!qualifierMatcher.matches()) {
                osgiVersion = s1 + s2 + "." + s3 + "_" + s4;
            }
        }

        /* convert
         * 1.string   -> 1.0.0.string
         * 1.2.string -> 1.2.0.string
         * 1          -> 1.0.0
         * 1.1        -> 1.1.0
         */
        //Pattern NEED_TO_FILL_ZEROS = Pattern.compile( "([0-9])(\\.([0-9]))?\\.([0-9A-Za-z_-]+)" );
        m = NEED_TO_FILL_ZEROS.matcher(osgiVersion);
        if (m.matches()) {
            String major = m.group(1);
            String minor = m.group(3);
            String service = null;
            String qualifier = m.group(5);

            /* if there's no qualifier just fill with 0s */
            if (qualifier == null) {
                osgiVersion = getVersion(major, minor, service, qualifier);
            } else {
                /* if last portion is only numbers then it's not a qualifier */
                Matcher qualifierMatcher = ONLY_NUMBERS.matcher(qualifier);
                if (qualifierMatcher.matches()) {
                    if (minor == null) {
                        minor = qualifier;
                    } else {
                        service = qualifier;
                    }

        int iEarliest = -1;
        String s = filter.regex ? filter.text : "\\Q" + filter.text + "\\E";
        Pattern pattern = Pattern.compile(s, Pattern.CASE_INSENSITIVE);
        for (int i = 0; i < cells.length; i++) {
          Matcher m = pattern.matcher(cells[i].getText());
          if (m.find() && (m.start() < iEarliest || iEarliest == -1)) {
            iEarliest = m.start();
            index = i;
          }
        }

        if (index < 0)

  public static String convertUrl(String source, Pattern pattern)
  {
    String result = source;

    Matcher  url_matcher = pattern.matcher(source);
    boolean found = url_matcher.find();
        if (found)
    {
      String visual_url = null;
      String actual_url = null;
      int last = 0;
            StringBuffer sb = new StringBuffer();
      synchronized (sb)
      {
        do
        {
          actual_url = url_matcher.group(1);
          if (url_matcher.groupCount() > 1)
          {
            visual_url = url_matcher.group(2);
          }
          else
          {
            visual_url = actual_url;
          }

          sb.append(source.substring(last, url_matcher.start(0)));
          sb.append("<a href=\"");
          if (actual_url.startsWith("www."))
          {
            actual_url = "http://"+actual_url;
          }
          sb.append(actual_url);
          sb.append("\"");
          sb.append(">");
          if (visual_url.length() <= 80)
          {
            sb.append(visual_url);
          }
          else
          {
            String ellipsis = "...";
            int last_slash = visual_url.lastIndexOf("/");
            int trailing_length = visual_url.length() - last_slash + ellipsis.length();
            int start_slash = visual_url.indexOf("/", visual_url.indexOf("://")+3);
            int previous_start_slash = start_slash;
            while (start_slash+trailing_length < 80)
            {
              previous_start_slash = start_slash;
              start_slash = visual_url.indexOf("/", previous_start_slash+1);
            }

            sb.append(visual_url.substring(0, previous_start_slash+1));
            sb.append(ellipsis);
            sb.append(visual_url.substring(last_slash));
          }
          sb.append("</a>");

          last = url_matcher.end(0);

          found = url_matcher.find();
        }
        while (found);

        sb.append(source.substring(last));
        result = sb.toString();

        }
      }
     
      encoded_message = convertUrl(encoded_message, URL_HIGHLIGHT);

      Matcher email_matcher = EMAIL_HIGHLIGHT.matcher(encoded_message);
      encoded_message = email_matcher.replaceAll("<a href=\"mailto:$1\">$1</a>");

      mTemplate.setValue("message", encoded_message);

      mTemplate.appendBlock("messages", "message");

        str = replaceNumericEntities(str, hexEntityPattern, 16);
        return replaceNumericEntities(str, decimalEntityPattern, 10);
    }

    private static String replaceNumericEntities(String str, Pattern pattern, int base) {
        Matcher matcher = pattern.matcher(str);
        StringBuffer buf = new StringBuffer(str.length());
        for (; matcher.find(); matcher.appendReplacement(buf, Character.toString((char) Integer.parseInt(matcher.group(1), base))))
            ;
        matcher.appendTail(buf);
        return buf.toString();
    }

            }
          }
         
          encoded_message = ChannelLog.convertUrl(encoded_message, ChannelLog.URL_HIGHLIGHT);

          Matcher email_matcher = ChannelLog.EMAIL_HIGHLIGHT.matcher(encoded_message);
          encoded_message = email_matcher.replaceAll("<a href=\"mailto:$1\">$1</a>");

          mTemplate.setValue("nickname", nickname);
          mTemplate.setValue("message", encoded_message);

          mTemplate.appendBlock("messages", "message");

                parsedNotation.getClassifier());
        return moduleDependency;
    }

    private ParsedModuleStringNotation splitDescriptionIntoModuleNotationAndArtifactType(String notation) {
        Matcher matcher = EXTENSION_SPLITTER.matcher(notation);
        boolean hasArtifactType = matcher.matches();
        if (hasArtifactType) {
            if (matcher.groupCount() != 2) {
                throw new InvalidUserDataException("The description " + notation + " is invalid");
            }
            return new ParsedModuleStringNotation(matcher.group(1), matcher.group(2));
        }
        return new ParsedModuleStringNotation(notation, null);
    }

   {
      Iterator<Pattern> iterator = map.keySet().iterator();
      while( iterator.hasNext() )
      {
         Pattern pattern = iterator.next();
         Matcher matcher = pattern.matcher( text );
         if( matcher.find( 0 ) )
         {
            return map.get( pattern );
         }
      }

   public List<FileLink> parseText( String text )
   {
      List<FileLink> fileLinks = new ArrayList<FileLink>();

      Pattern combinedSearchPattern = fileLinkDefinitionLord.getSearchPattern();
      Matcher matcher = combinedSearchPattern.matcher( text );

      int index = 0;

      boolean foundAMatch = matcher.find( index );
      while( foundAMatch )
      {
         // Retrieve matching string
         String matchedText = matcher.group();

         // Retrieve indices of matching string
         int start = matcher.start();
         int end = matcher.end();

         int nextStarting = start;

         //now that we have a match, we have to find the one FileLinkDefinition that actually matches so it
         //can determine the actual file. This makes the matcher more plugable.
         FileLinkDefinition fileLinkDefinition = fileLinkDefinitionLord.getMatchingFileLinkDefinition( matchedText );
         if( fileLinkDefinition != null )
         {
            nextStarting = fileLinkDefinition.parseFileLink( text, matchedText, start, end, verifyFileExists, fileLinks );
         }
         else
         {
            //this is probably a serious problem that needs to be reported. However, we'll continue as if nothing bad happened.
            System.out.println( "We found a match but didn't find the matching definition. Matched text:\n" + text );
         }

         if( nextStarting == -1 || nextStarting < start )
         {
            nextStarting = start;
         }

         index = nextStarting + 1;
         if( index < text.length() )
         {
            foundAMatch = matcher.find( index );
         }
         else
         {
            foundAMatch = false; //don't continue searching if we've found the end
         }

TOP

Related Classes of java.util.regex.Matcher

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.