Example usage for java.util.regex Matcher end

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

Introduction

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

Prototype

public int end() 

Source Link

Document

Returns the offset after the last character matched.

Usage

From source file:org.canova.api.conf.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }/* w  w  w.  j  a va  2 s . c  o m*/
    Matcher match = varPat.matcher("");
    String eval = expr;
    int MAX_SUBST = 20;
    for (int s = 0; s < MAX_SUBST; s++) {
        match.reset(eval);
        if (!match.find()) {
            return eval;
        }
        String var = match.group();
        var = var.substring(2, var.length() - 1); // remove ${ .. }
        String val = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        if (val == null) {
            val = getRaw(var);
        }
        if (val == null) {
            return eval; // return literal ${var}: var is unbound
        }
        // substitute
        eval = eval.substring(0, match.start()) + val + eval.substring(match.end());
    }
    throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr);
}

From source file:at.tuwien.ldlab.statspace.metadata.MetaDataForSPARQL.java

public void createMetaDataForTime() {
    int i, j, n = times.size();
    String sInterval = "[1-9][0-9]{3}-[1-9][0-9]{3}";
    String sYear = "[1-9][0-9]{3}";
    String sMonth = "[1-9][0-9]{3}-[0-1][0-9]";
    String sQuarter = "[1-9][0-9]{3}-Q[1-4]";
    String sDate = "[1-9][0-9]{3}-[0-1][0-9]-[0-3][0-9]";
    String value, timeValue = "";

    Pattern pInterval = Pattern.compile(sInterval);
    Pattern pYear = Pattern.compile(sYear);
    Pattern pQuarter = Pattern.compile(sQuarter);
    Pattern pMonth = Pattern.compile(sMonth);
    Pattern pDate = Pattern.compile(sDate);

    Resource rIntervalType = mOutput.createResource("http://rdfs.org/ns/void#Dataset");
    Resource rMonthType = mOutput.createResource(intervals + "CalendarMonth");
    Resource rDateType = mOutput.createResource(intervals + "CalendarDay");
    Resource rYearType = mOutput.createResource(intervals + "CalendarYear");
    Resource rQuarterType = mOutput.createResource(intervals + "CalendarQuarter");
    Property pSameAs = mOutput.createProperty(owl + "sameAs");

    ArrayList<String> arrDate = new ArrayList<String>(); //array to store value of date
    Boolean bDate = false, bOther = false;

    Matcher m;

    for (i = 0; i < n; i++) {
        value = times.get(i);/*from   www . j  a v a 2 s.  c o  m*/
        if (value.startsWith(id + "gregorian"))
            continue;

        if (value.toLowerCase().contains("year")) {
            //interval
            m = pInterval.matcher(value);
            if (m.find()) {
                bOther = true;
                timeValue = value.substring(m.start(), m.end());
                String sFrom = timeValue.substring(0, 4);
                String sEnd = timeValue.substring(5);
                int yFrom = Integer.parseInt(sFrom);
                int yEnd = Integer.parseInt(sEnd);
                timeValue = Integer.toString(yFrom) + "-01-01T00:00:00/P" + Integer.toString(yEnd - yFrom)
                        + "Y";
                Resource rUKTime = mOutput.createResource(id + "gregorian-interval/" + timeValue);
                rUKTime.addProperty(RDF.type, rIntervalType);
                if (value.startsWith("http")) {
                    Resource rValue = mOutput.createResource(value);
                    if (rValue.getProperty(RDFS.label) == null)
                        rValue.addProperty(RDFS.label, timeValue);
                    rUKTime.addProperty(pSameAs, rValue);
                } else {
                    rUKTime.addProperty(pSameAs, value);
                }
            } else {
                //year
                m = pYear.matcher(value);
                if (m.find()) {
                    bOther = true;
                    timeValue = value.substring(m.start(), m.end());
                    Resource rUKTime = mOutput.createResource(id + "gregorian-year/" + timeValue);
                    rUKTime.addProperty(RDF.type, rYearType);
                    if (value.startsWith("http")) {
                        Resource rValue = mOutput.createResource(value);
                        if (rValue.getProperty(RDFS.label) == null)
                            rValue.addProperty(RDFS.label, timeValue);
                        rUKTime.addProperty(pSameAs, rValue);
                    } else {
                        rUKTime.addProperty(pSameAs, value);
                    }
                    createMetaDataForYear(timeValue);
                } else
                    System.out.println("Not found + " + value + "\t index: " + i);
            }
        } else if (value.toLowerCase().contains("quarter")) {
            m = pQuarter.matcher(value);
            if (m.find()) {
                bOther = true;
                timeValue = value.substring(m.start(), m.end());
                Resource rUKTime = mOutput.createResource(id + "gregorian-quarter/" + timeValue);
                rUKTime.addProperty(RDF.type, rQuarterType);
                if (value.startsWith("http")) {
                    Resource rValue = mOutput.createResource(value);
                    if (rValue.getProperty(RDFS.label) == null)
                        rValue.addProperty(RDFS.label, timeValue);
                    rUKTime.addProperty(pSameAs, rValue);
                } else {
                    rUKTime.addProperty(pSameAs, value);
                }
                createMetaDataForQuarter(timeValue);

            } else
                System.out.println("Not found + " + value + "\t index: " + i);
        } else if (value.toLowerCase().contains("month")) {
            m = pMonth.matcher(value);
            if (m.find()) {
                bOther = true;
                timeValue = value.substring(m.start(), m.end());
                Resource rUKTime = mOutput.createResource(id + "gregorian-month/" + timeValue);
                rUKTime.addProperty(RDF.type, rMonthType);
                if (value.startsWith("http")) {
                    Resource rValue = mOutput.createResource(value);
                    if (rValue.getProperty(RDFS.label) == null)
                        rValue.addProperty(RDFS.label, timeValue);
                    rUKTime.addProperty(pSameAs, rValue);
                } else {
                    rUKTime.addProperty(pSameAs, value);
                }
            } else
                System.out.println("Not found + " + value + "\t index: " + i);
        } else if (value.toLowerCase().contains("date")) {
            m = pDate.matcher(value);
            if (m.find()) {
                bDate = true;
                arrDate.add(value);
            } else
                System.out.println("Not found + " + value + "\t index: " + i);
        } else {
            m = pDate.matcher(value);
            if (m.find()) {
                //date
                bDate = true;
                arrDate.add(value);
            } else {
                //quarter                        
                m = pQuarter.matcher(value);
                if (m.find()) {
                    bOther = true;
                    timeValue = value.substring(m.start(), m.end());
                    Resource rUKTime = mOutput.createResource(id + "gregorian-quarter/" + timeValue);
                    rUKTime.addProperty(RDF.type, rQuarterType);
                    if (value.startsWith("http")) {
                        Resource rValue = mOutput.createResource(value);
                        if (rValue.getProperty(RDFS.label) == null)
                            rValue.addProperty(RDFS.label, timeValue);
                        rUKTime.addProperty(pSameAs, rValue);
                    } else {
                        rUKTime.addProperty(pSameAs, value);
                    }
                    createMetaDataForQuarter(timeValue);
                } else {
                    //month                  
                    m = pMonth.matcher(value);
                    if (m.find()) {
                        bOther = true;
                        timeValue = value.substring(m.start(), m.end());
                        Resource rUKTime = mOutput.createResource(id + "gregorian-month/" + timeValue);
                        rUKTime.addProperty(RDF.type, rMonthType);
                        if (value.startsWith("http")) {
                            Resource rValue = mOutput.createResource(value);
                            if (rValue.getProperty(RDFS.label) == null)
                                rValue.addProperty(RDFS.label, timeValue);
                            rUKTime.addProperty(pSameAs, rValue);
                        } else {
                            rUKTime.addProperty(pSameAs, value);
                        }
                    } else {
                        //interval
                        m = pInterval.matcher(value);
                        if (m.find()) {
                            bOther = true;
                            timeValue = value.substring(m.start(), m.end());
                            String sFrom = timeValue.substring(0, 4);
                            String sEnd = timeValue.substring(5);
                            int yFrom = Integer.parseInt(sFrom);
                            int yEnd = Integer.parseInt(sEnd);
                            timeValue = Integer.toString(yFrom) + "-01-01T00:00:00/P"
                                    + Integer.toString(yEnd - yFrom) + "Y";
                            Resource rUKTime = mOutput.createResource(id + "gregorian-interval/" + timeValue);
                            rUKTime.addProperty(RDF.type, rIntervalType);
                            if (value.startsWith("http")) {
                                Resource rValue = mOutput.createResource(value);
                                if (rValue.getProperty(RDFS.label) == null)
                                    rValue.addProperty(RDFS.label, timeValue);
                                rUKTime.addProperty(pSameAs, rValue);
                            } else {
                                rUKTime.addProperty(pSameAs, value);
                            }
                        } else {
                            //year                     
                            m = pYear.matcher(value);
                            if (m.find()) {
                                bOther = true;
                                timeValue = value.substring(m.start(), m.end());
                                Resource rUKTime = mOutput.createResource(id + "gregorian-year/" + timeValue);
                                rUKTime.addProperty(RDF.type, rYearType);
                                if (value.startsWith("http")) {
                                    Resource rValue = mOutput.createResource(value);
                                    if (rValue.getProperty(RDFS.label) == null)
                                        rValue.addProperty(RDFS.label, timeValue);
                                    rUKTime.addProperty(pSameAs, rValue);
                                } else {
                                    rUKTime.addProperty(pSameAs, value);
                                }
                                createMetaDataForYear(timeValue);
                            } else
                                System.out.println("Not found + " + value + "\t index: " + i);
                        }
                    }

                }
            }
        }
    }

    if (bDate == true && bOther == false && arrDate.size() > 0) {
        //if every value in this dataset has format YYYY-01-01 or YYYY-12-31 => only get Year         
        for (i = 0; i < arrDate.size(); i++)
            if (!(arrDate.get(i).contains("-01-01") || arrDate.get(i).contains("-12-31")))
                break;

        if (i < arrDate.size()) {
            for (j = 0; j < arrDate.size(); j++) {
                value = arrDate.get(j);
                m = pDate.matcher(value);
                if (m.find()) {
                    timeValue = value.substring(m.start(), m.end());
                    Resource rUKTime = mOutput.createResource(id + "gregorian-date/" + timeValue);
                    rUKTime.addProperty(RDF.type, rDateType);
                    if (value.startsWith("http")) {
                        Resource rValue = mOutput.createResource(value);
                        rValue.addProperty(RDFS.label, timeValue);
                        rUKTime.addProperty(pSameAs, rValue);
                    } else {
                        rUKTime.addProperty(pSameAs, value);
                    }
                }
            }
        } else {
            for (i = 0; i < arrDate.size(); i++) {
                value = arrDate.get(i);
                m = pYear.matcher(value);
                if (m.find()) {
                    timeValue = value.substring(m.start(), m.end());
                    Resource rUKTime = mOutput.createResource(id + "gregorian-year/" + timeValue);
                    rUKTime.addProperty(RDF.type, rYearType);
                    if (value.startsWith("http")) {
                        Resource rValue = mOutput.createResource(value);
                        rValue.addProperty(RDFS.label, timeValue);
                        rUKTime.addProperty(pSameAs, rValue);
                    } else {
                        rUKTime.addProperty(pSameAs, value);
                    }
                    createMetaDataForYear(timeValue);
                }
            }
        }
    }
}

From source file:conf.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }//from   w w  w.  j  a v  a2 s.  c om
    Matcher match = varPat.matcher("");
    String eval = expr;
    for (int s = 0; s < MAX_SUBST; s++) {
        match.reset(eval);
        if (!match.find()) {
            return eval;
        }
        String var = match.group();
        var = var.substring(2, var.length() - 1); // remove ${ .. }
        String val = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        if (val == null) {
            val = getRaw(var);
        }
        if (val == null) {
            return eval; // return literal ${var}: var is unbound
        }
        // substitute
        eval = eval.substring(0, match.start()) + val + eval.substring(match.end());
    }
    throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr);
}

From source file:cn.suishen.email.activity.MessageViewFragmentBase.java

/**
 * Reload the body from the provider cursor.  This must only be called from the UI thread.
 *
 * @param bodyText text part/*w  w  w.jav  a  2s  .  c om*/
 * @param bodyHtml html part
 *
 * TODO deal with html vs text and many other issues <- WHAT DOES IT MEAN??
 */
private void reloadUiFromBody(String bodyText, String bodyHtml, boolean autoShowPictures) {
    String text = null;
    mHtmlTextRaw = null;
    boolean hasImages = false;

    if (bodyHtml == null) {
        text = bodyText;
        /*
         * Convert the plain text to HTML
         */
        StringBuffer sb = new StringBuffer("<html><body>");
        if (text != null) {
            // Escape any inadvertent HTML in the text message
            text = EmailHtmlUtil.escapeCharacterToDisplay(text);
            // Find any embedded URL's and linkify
            Matcher m = Patterns.WEB_URL.matcher(text);
            while (m.find()) {
                int start = m.start();
                /*
                 * WEB_URL_PATTERN may match domain part of email address. To detect
                 * this false match, the character just before the matched string
                 * should not be '@'.
                 */
                if (start == 0 || text.charAt(start - 1) != '@') {
                    String url = m.group();
                    Matcher proto = WEB_URL_PROTOCOL.matcher(url);
                    String link;
                    if (proto.find()) {
                        // This is work around to force URL protocol part be lower case,
                        // because WebView could follow only lower case protocol link.
                        link = proto.group().toLowerCase() + url.substring(proto.end());
                    } else {
                        // Patterns.WEB_URL matches URL without protocol part,
                        // so added default protocol to link.
                        link = "http://" + url;
                    }
                    String href = String.format("<a href=\"%s\">%s</a>", link, url);
                    m.appendReplacement(sb, href);
                } else {
                    m.appendReplacement(sb, "$0");
                }
            }
            m.appendTail(sb);
        }
        sb.append("</body></html>");
        text = sb.toString();
    } else {
        text = bodyHtml;
        mHtmlTextRaw = bodyHtml;
        hasImages = IMG_TAG_START_REGEX.matcher(text).find();
    }

    // TODO this is not really accurate.
    // - Images aren't the only network resources.  (e.g. CSS)
    // - If images are attached to the email and small enough, we download them at once,
    //   and won't need network access when they're shown.
    if (hasImages) {
        if (mRestoredPictureLoaded || autoShowPictures) {
            blockNetworkLoads(false);
            addTabFlags(TAB_FLAGS_PICTURE_LOADED); // Set for next onSaveInstanceState

            // Make sure to reset the flag -- otherwise this will keep taking effect even after
            // moving to another message.
            mRestoredPictureLoaded = false;
        } else {
            addTabFlags(TAB_FLAGS_HAS_PICTURES);
        }
    }
    setMessageHtml(text);

    // Ask for attachments after body
    new LoadAttachmentsTask().executeParallel(mMessage.mId);

    mIsMessageLoadedForTest = true;
}

From source file:org.ala.dao.FulltextSearchDaoImplSolr.java

/**
 * Applies a prefix and suffix to higlight the search terms in the
 * supplied list./*  ww  w.  java 2  s .c  o  m*/
 *
 * NC: This is a workaround as I can not get SOLR highlighting to work for partial term matches.
 *
 * @param names
 * @param m
 * @return
 */
private List<String> getHighlightedNames(List<String> names, java.util.regex.Matcher m, String prefix,
        String suffix) {
    LinkedHashSet<String> hlnames = null;
    List<String> lnames = null;
    if (names != null) {
        hlnames = new LinkedHashSet<String>();
        for (String name : names) {
            String name1 = SolrUtils.concatName(name.trim());
            m.reset(name1);
            if (m.find()) {
                //insert <b> and </b>at the start and end index
                name = name.substring(0, m.start()) + prefix + name.substring(m.start(), m.end()) + suffix
                        + name.substring(m.end(), name.length());
                hlnames.add(name);
            }
        }
        if (!hlnames.isEmpty()) {
            lnames = new ArrayList<String>(hlnames);
            Collections.sort(lnames);
        } else {
            lnames = new ArrayList<String>();
        }
    }
    return lnames;
}

From source file:org.ala.dao.FulltextSearchDaoImplSolr.java

/**
 * if word highlight enabled then do the exact match, otherwise do the concat match
 * /* ww  w .j av  a2s.  co  m*/
 * @param names
 * @param term
 * @param prefix
 * @param suffix
 * @return
 */
private List<String> getHighlightedNames(List<String> names, String term, String prefix, String suffix) {
    LinkedHashSet<String> hlnames = null;
    List<String> lnames = null;
    String value = null;
    boolean isHighlight = false;

    //have word highlight
    if (prefix != null && suffix != null && prefix.trim().length() > 0 && suffix.trim().length() > 0
            && term != null) {
        value = SolrUtils.cleanName(term.trim());
        isHighlight = true;
    } else {
        value = SolrUtils.concatName(term);
    }
    Pattern p = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
    java.util.regex.Matcher m = p.matcher(value);
    if (names != null) {
        hlnames = new LinkedHashSet<String>();
        for (String name : names) {
            String name1 = null;
            name = name.trim();
            if (isHighlight) {
                name1 = name;
            } else {
                name1 = SolrUtils.concatName(name);
            }
            m.reset(name1);
            if (m.find()) {
                //insert <b> and </b>at the start and end index
                name = name.substring(0, m.start()) + prefix + name.substring(m.start(), m.end()) + suffix
                        + name.substring(m.end(), name.length());
                hlnames.add(name);
            }
        }
        if (!hlnames.isEmpty()) {
            lnames = new ArrayList<String>(hlnames);
            Collections.sort(lnames);
        } else {
            lnames = new ArrayList<String>();
        }
    }
    return lnames;
}

From source file:gtu._work.ui.RegexReplacer.java

/**
 * @param fromPattern//from www  .j a v  a 2 s .co m
 *            ???pattern
 * @param toFormat
 *            ??pattern
 * @param replaceText
 *            ??
 */
String replacer(String fromPattern, String toFormat, String replaceText) {
    String errorRtn = replaceText.toString();
    try {
        int patternFlag = 0;

        // 
        if (multiLineCheckBox.isSelected()) {
            patternFlag = Pattern.DOTALL | Pattern.MULTILINE;
        }

        Pattern pattern = Pattern.compile(fromPattern, patternFlag);
        Matcher matcher = pattern.matcher(replaceText);

        StringBuffer sb = new StringBuffer();
        String tempStr = null;

        TradeOffConfig config = this.getTradeOffConfig();

        {
            int startPos = 0;
            for (; matcher.find();) {
                tempStr = toFormat.toString();
                sb.append(replaceText.substring(startPos, matcher.start()));

                // ----------------------------------------------
                if (StringUtils.isBlank(config.fremarkerKey)) {
                    // regex
                    for (int ii = 0; ii <= matcher.groupCount(); ii++) {
                        System.out.println(ii + " -- " + matcher.group(ii));
                        tempStr = tempStr.replaceAll("#" + ii + "#",
                                Matcher.quoteReplacement(matcher.group(ii)));
                    }
                } else if (StringUtils.isNotBlank(config.fremarkerKey)) {
                    // freemarker
                    Map<String, Object> root = new HashMap<String, Object>();
                    TreeMap<Integer, Object> lstMap = new TreeMap<Integer, Object>();
                    for (int ii = 0; ii <= matcher.groupCount(); ii++) {
                        lstMap.put(ii, matcher.group(ii));
                    }
                    root.put(StringUtils.trimToEmpty(config.fremarkerKey), lstMap.values());
                    System.out.println("template Map : " + root);
                    tempStr = FreeMarkerSimpleUtil.replace(tempStr, root);
                }
                // ----------------------------------------------

                sb.append(tempStr);
                startPos = matcher.end();
            }
            sb.append(replaceText.substring(startPos));
        }

        return sb.toString();
    } catch (Exception ex) {
        JOptionPaneUtil.newInstance().iconErrorMessage().showMessageDialog(ex.getMessage(), getTitle());
        return errorRtn;
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.regexp.HtmlUnitRegExpProxy.java

String computeReplacementValue(final String replacement, final String originalString, final Matcher matcher) {

    int lastIndex = 0;
    final StringBuilder result = new StringBuilder();
    int i;//from  ww w.j  av  a 2  s  .  c om
    while ((i = replacement.indexOf('$', lastIndex)) > -1) {
        if (i > 0) {
            result.append(replacement, lastIndex, i);
        }
        String ss = null;
        if (i < replacement.length() - 1 && (i == lastIndex || replacement.charAt(i - 1) != '$')) {
            final char next = replacement.charAt(i + 1);
            // only valid back reference are "evaluated"
            if (next >= '1' && next <= '9') {
                final int num1digit = next - '0';
                final char next2 = (i + 2 < replacement.length()) ? replacement.charAt(i + 2) : 'x';
                final int num2digits;
                // if there are 2 digits, the second one is considered as part of the group number
                // only if there is such a group
                if (next2 >= '1' && next2 <= '9') {
                    num2digits = num1digit * 10 + (next2 - '0');
                } else {
                    num2digits = Integer.MAX_VALUE;
                }
                if (num2digits <= matcher.groupCount()) {
                    ss = matcher.group(num2digits);
                    i++;
                } else if (num1digit <= matcher.groupCount()) {
                    ss = StringUtils.defaultString(matcher.group(num1digit));
                }
            } else {
                switch (next) {
                case '&':
                    ss = matcher.group();
                    break;
                case '0':
                    if (browserVersion_.hasFeature(JS_REGEXP_GROUP0_RETURNS_WHOLE_MATCH)) {
                        ss = matcher.group();
                    }
                    break;
                case '`':
                    ss = originalString.substring(0, matcher.start());
                    break;
                case '\'':
                    ss = originalString.substring(matcher.end());
                    break;
                case '$':
                    ss = "$";
                    break;
                default:
                }
            }
        }
        if (ss == null) {
            result.append('$');
            lastIndex = i + 1;
        } else {
            result.append(ss);
            lastIndex = i + 2;
        }
    }

    result.append(replacement, lastIndex, replacement.length());

    return result.toString();
}

From source file:au.org.theark.core.service.ArkCommonServiceImpl.java

public String generateNaturalUID(String UID) {
    StringBuilder natBuilder = new StringBuilder();
    Matcher matcher = Pattern.compile("\\d+").matcher(UID);
    int last_end = 0;
    while (matcher.find()) {
        if (matcher.start() > last_end) {
            natBuilder.append(UID.substring(last_end, matcher.start()));
        }//w  ww.  ja  v  a2  s.  c o m
        String subjectUIDNumber = StringUtils.leftPad(UID.substring(matcher.start(), matcher.end()), 20, '0');
        natBuilder.append(subjectUIDNumber);
        last_end = matcher.end();
    }
    return natBuilder.toString();
}

From source file:com.glaf.core.config.Configuration.java

private String substituteVars(String expr) {
    if (expr == null) {
        return null;
    }/*w w  w  . j  a  va2 s  .c  o  m*/
    Matcher match = VAR_PATTERN.matcher("");
    String eval = expr;
    for (int s = 0; s < MAX_SUBST; s++) {
        match.reset(eval);
        if (!match.find()) {
            return eval;
        }
        String var = match.group();
        var = var.substring(2, var.length() - 1); // remove ${ .. }
        String val = null;
        try {
            val = System.getProperty(var);
        } catch (SecurityException se) {
            LOG.warn("Unexpected SecurityException in Configuration", se);
        }
        if (val == null) {
            val = getRaw(var);
        }
        if (val == null) {
            return eval; // return literal ${var}: var is unbound
        }
        // substitute
        eval = eval.substring(0, match.start()) + val + eval.substring(match.end());
    }
    throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr);
}