Example usage for java.util.regex Matcher start

List of usage examples for java.util.regex Matcher start

Introduction

In this page you can find the example usage for java.util.regex Matcher start.

Prototype

public int start() 

Source Link

Document

Returns the start index of the previous match.

Usage

From source file:com.github.mavenplugins.doctest.ReportMojo.java

/**
 * Gets the javadoc comment situated over a doctest method.
 *//*from  w  ww .j av  a2  s .  c om*/
protected String getJavaDoc(String source, String method) {
    Pattern methodPattern = Pattern.compile(
            "public\\s+void\\s+" + method + "\\s*\\((HttpResponse|"
                    + HttpResponse.class.getName().replaceAll("\\.", "\\\\.") + ")",
            Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    Matcher matcher = methodPattern.matcher(source);
    int start, tmp, last, comment;
    String doc;

    if (matcher.find()) {
        start = matcher.start();
        last = -1;
        matcher = ANY_METHOD_FINDER.matcher(source);
        while (matcher.find() && (tmp = matcher.start()) < start) {
            last = tmp;
        }

        comment = source.lastIndexOf("/**", start);

        if (comment > 2 && (comment > last || last == -1)) {
            doc = source.substring(comment, source.indexOf("*/", comment));
            doc = doc.substring(3, doc.length() - 2);
            doc = JAVADOC_EMPTYLINE_FINDER.matcher(doc).replaceAll(LINE_SEPARATOR);
            doc = JAVADOC_STAR_FINDER.matcher(doc).replaceAll("");
            doc = StringUtils.replace(doc, " ", "&nbsp;");
            doc = StringUtils.replace(doc, LINE_SEPARATOR, "<br/>");
            return doc;
        }
    }

    return "";
}

From source file:com.bidorbuy.graylog.alarmcallbacks.jira.JiraAlarmCallback.java

/**
 * Generates the MD5 digest of either the message or a number of fields provided
 * @param stream//from   w ww .  jav a2 s .c  om
 * @param checkResult
 * @return
 */
private String getJIRAMessageDigest(final Stream stream, final AlertCondition.CheckResult result) {

    String JiraMessageDigest = "";

    // Get the last message
    if (!result.getMatchingMessages().isEmpty()) {
        String JiraMessageRegex = "";
        MessageSummary lastMessage = result.getMatchingMessages().get(0);

        // Let's extract the message regex first
        if (configuration.stringIsSet(CK_MESSAGE_REGEX)
                && !configuration.getString(CK_MESSAGE_REGEX).equals("null")) {
            try {
                Matcher matcher = Pattern.compile(configuration.getString(CK_MESSAGE_REGEX))
                        .matcher(lastMessage.getMessage());

                if (matcher.find()) {
                    JiraMessageRegex = lastMessage.getMessage().substring(matcher.start());
                }
            } catch (Exception ex) {
                LOG.warn("Error in JIRA-issue MD5-MESSAGE_REGEX generation: " + ex.getMessage());
            }
        }

        String JiraMD5Content = "";

        // Let's extract the message regex first
        if (configuration.stringIsSet(CK_JIRA_MD5_HASH_PATTERN)
                && !configuration.getString(CK_JIRA_MD5_HASH_PATTERN).equals("null")) {

            try {
                JiraMD5Content = configuration.getString(CK_JIRA_MD5_HASH_PATTERN);

                // replace the message-regex place-holder
                JiraMD5Content = JiraMD5Content.replace("[MESSAGE_REGEX]", JiraMessageRegex);

                // iterate through all the message fields and replace the template
                Map<String, Object> lastMessageFields = lastMessage.getFields();

                for (Map.Entry<String, Object> arg : lastMessageFields.entrySet()) {
                    JiraMD5Content = JiraMD5Content.replace("[LAST_MESSAGE." + arg.getKey() + "]",
                            arg.getValue().toString());
                }

                // We regex template fields which have not been replaced
                JiraMD5Content = JiraMD5Content.replaceAll("\\[LAST_MESSAGE\\.[^\\]]*\\]", "");
            } catch (Exception ex) {
                LOG.warn("Error in JIRA-issue MD5-HASH_PATTERN generation: " + ex.getMessage());
            }
        }

        // We default the extracted message as the template
        if (StringUtils.isBlank(JiraMD5Content)) {
            JiraMD5Content = JiraMessageRegex;
        }

        // Create the MD5 from the template
        if (StringUtils.isNotBlank(JiraMD5Content)) {
            try {
                MessageDigest m = MessageDigest.getInstance("MD5");
                m.update(JiraMD5Content.getBytes(), 0, JiraMD5Content.length());
                JiraMessageDigest = new BigInteger(1, m.digest()).toString(16);
            } catch (Exception ex) {
                LOG.warn("Error in JIRA-issue MD5 generation (MD5-string=" + JiraMD5Content + "): "
                        + ex.getMessage());
            }
        } else {
            LOG.warn("Skipped MD5-hash creation, MD5-string is empty. Check your config");
        }
    } else {
        LOG.warn("Skipping JIRA-issue MD5 generation, alarmcallback did not provide a message");
    }

    return JiraMessageDigest;
}

From source file:com.g3net.tool.StringUtils.java

/**
 * ???/*from  w w  w.j a v a2  s .  c om*/
 * 
 * @param src
 * @param regexp
 * @param ignoreCase
 * @param startPos
 *            ??
 * @return
 */
public static boolean endsWith(String src, String regexp, boolean ignoreCase, TInteger startPos) {

    Pattern p = null;
    if (ignoreCase) {
        p = Pattern.compile(regexp + "$", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    } else {
        p = Pattern.compile(regexp + "$", Pattern.MULTILINE);
    }
    Matcher m = p.matcher(src);
    while (m.find()) {
        // log.info(m.group()+":"+m.start()+":"+m.end());
        startPos.setValue(m.start());
        return true;
    }
    return false;
}

From source file:com.sbcc.edu.jrollspellchecker.MainWindow.java

private void indexTest(ArrayList<IndexedWord> indexedWords, int startIndex) {
    String regex = "\\w*";
    Pattern p = Pattern.compile(regex);

    //System.out.println("Text is : " + spellCheckText.getText());
    Matcher matcher = p.matcher(spellCheckText.getText());
    while (matcher.find()) {
        System.out.println("Starting & ending  index of" + matcher.group() + ":=" + "start=" + matcher.start()
                + " end = " + matcher.end());
        if (matcher.start() >= startIndex)
            indexedWords.add(new IndexedWord(matcher.start(), matcher.end(), matcher.group()));
    }/*from www  .  j a v  a  2  s .c om*/
}

From source file:org.jspringbot.keyword.db.DbHelper.java

private String sqlSubstitute(String queryString) {
    StringBuilder buf = new StringBuilder(queryString);

    Matcher matcher = NAMED_PARAMETER_PATTERN.matcher(buf);

    int index = 0;
    while (matcher.find(index)) {
        String param = matcher.group().substring(1);
        if (literalSubstitution.containsKey(param)) {
            String replacement = literalSubstitution.get(param);
            buf.replace(matcher.start(), matcher.end(), replacement);

            index = matcher.start() + replacement.length();
        } else {/*from  ww  w  . ja  v  a 2s . c  om*/
            index = matcher.end();
        }
    }

    return buf.toString();
}

From source file:com.g3net.tool.StringUtils.java

public static int lastIndexOf(String srcStr, String regexp, boolean ignoreCase, TInteger endPos) {

    Pattern p = null;//ww  w.  j av a  2 s.c  o  m
    if (ignoreCase) {
        p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
    } else {
        p = Pattern.compile(regexp);
    }
    Matcher m = p.matcher(srcStr);
    int end = -1;
    while (m.find()) {
        // log.info(m.group()+":"+m.start()+":"+m.end());
        end = m.start();
        endPos.setValue(m.end());
    }
    return end;
    // sql3.regionMatches(ignoreCase, toffset, other, ooffset, len)
    // log.info(m.matches());
}

From source file:com.g3net.tool.StringUtils.java

/**
 * ??// ww w.  ja  v a 2 s  .  c o  m
 * 
 * @param srcStr
 * @param regexp
 * @param ignoreCase
 *            ??
 * @return
 */
public static int lastIndexOf(String srcStr, String regexp, boolean ignoreCase) {

    Pattern p = null;
    if (ignoreCase) {
        p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
    } else {
        p = Pattern.compile(regexp);
    }
    Matcher m = p.matcher(srcStr);
    int end = -1;
    while (m.find()) {
        // log.info(m.group()+":"+m.start()+":"+m.end());
        end = m.start();

    }
    return end;
    // sql3.regionMatches(ignoreCase, toffset, other, ooffset, len)
    // log.info(m.matches());
}

From source file:com.ficeto.esp.EspExceptionDecoder.java

private void parseText() {
    String content = inputArea.getText();
    Pattern p = Pattern.compile("40[0-2](\\d|[a-f]){5}\\b");
    int count = 0;
    Matcher m = p.matcher(content);
    while (m.find()) {
        count++;//w w w  . j  ava2  s.c o m
    }
    if (count == 0) {
        return;
    }
    String command[] = new String[4 + count];
    int i = 0;
    command[i++] = tool.getAbsolutePath();
    command[i++] = "-aipfC";
    command[i++] = "-e";
    command[i++] = elf.getAbsolutePath();
    m = p.matcher(content);
    while (m.find()) {
        command[i++] = content.substring(m.start(), m.end());
    }
    outputText += "<i>Decoding " + count + " results</i>\n";
    sysExec(command);
}

From source file:com.g3net.tool.StringUtils.java

public static int indexOf(String srcStr, String regexp, boolean ignoreCase, TInteger endPos) {

    Pattern p = null;/*ww  w  . j a  v  a  2  s .  c  o m*/
    if (ignoreCase) {
        p = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
    } else {
        p = Pattern.compile(regexp);
    }
    Matcher m = p.matcher(srcStr);
    int startPos = -1;
    while (m.find()) {
        // log.info(m.group()+":"+m.start()+":"+m.end());
        startPos = m.start();
        endPos.setValue(m.end());
        return startPos;

    }
    return -1;
    // sql3.regionMatches(ignoreCase, toffset, other, ooffset, len)
    // log.info(m.matches());
}

From source file:de.dfki.km.perspecting.obie.model.Document.java

/***************************************************************************
 * Gets the pure plain text out of a html text. All html tags are replaced
 * by spaces. To do so, the head is replaced, all remaining javascript tags
 * (including the content) and finally all remaining html tags. Thus,
 * absolute positioning is possible./*w w  w  .  jav a 2s.c om*/
 * 
 * @param text
 *            content of the html document as text
 * @return text where all html was replaced by spaces
 */
private String extractPlainTextFromHtml(String text) {
    Collection<Pattern> patterns = new ArrayList<Pattern>(3);
    // Delete the head, then all remaining javascript items that might exist
    // in the body, then all remaining html tags.
    patterns.add(
            Pattern.compile("<head.*/head>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL));
    // .*? makes it non greedy -> take the shortes match
    // DOTALL does also include new lines
    patterns.add(Pattern.compile("<script.*?/script>",
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL));
    patterns.add(Pattern.compile("<.+?>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
    StringBuffer s = new StringBuffer(text);

    // Go for all patterns.
    for (Pattern p : patterns) {
        Matcher matcher = p.matcher(s);

        // As long as the matcher finds another occurance of the pattern we
        // replace it by the same number of spaces but keep new lines.
        while (matcher.find())
            s.replace(matcher.start(), matcher.end(), matcher.group().replaceAll(".", " "));
    }
    return s.toString();
}