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.redhat.rhn.common.util.StringUtil.java

/**
 * Returns a String for html parsing escapes html converts \n to a break tag
 * (<BR/<) converts urls beginning with http:// and https:// to links
 * Example: given http://foo.bar/example return <a
 * href="http://foo.bar/example">http://foo.bar/example</a>
 * @param convertIn the String we want to convert
 * @return html version of the String/*from  w w  w .ja  v  a2 s .  c o m*/
 * @see org.apache.commons.lang.StringEscapeUtils
 */
public static String htmlifyText(String convertIn) {
    if (convertIn == null) {
        return null;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("htmlifyText() - " + convertIn);
    }
    String retval = StringEscapeUtils.escapeHtml(convertIn);
    retval = retval.replaceAll("\\\\r\\\\n", "<br/>");
    retval = retval.replaceAll("\\\\n", "<br/>");
    retval = retval.replaceAll("\r\n", "<br/>");
    retval = retval.replaceAll("\n", "<br/>");

    Pattern startUrl = Pattern.compile("https?://"); // http:// or https://
    Matcher next = startUrl.matcher(retval);
    boolean done = false;
    int previous = 0; // the starting index of the previously found url
    List<String> pieces = new LinkedList<String>();

    /*
     * Separates the string into a list. Break points for different tokens
     * in the list are the start of each hyperlink (http:// or https://).
     * Basically, this finds the start of each piece we need to modify
     */
    while (!done) {
        if (next.find()) {
            pieces.add(retval.substring(previous, next.start()));
            previous = next.start();
        } else {
            pieces.add(retval.substring(previous));
            done = true;
        }
    }

    /*
     * Adds each piece of the list back to the string modifying each that
     * starts with http:// or https:// This part finds the end of each url
     * and executes modifications
     */
    Iterator<String> itr = pieces.iterator();
    StringBuilder result = new StringBuilder();
    while (itr.hasNext()) {
        String current = itr.next();
        Matcher match = startUrl.matcher(current);
        if (match.find()) { // if this is a url
            int end = findEndOfUrl(current);
            StringBuilder modify = new StringBuilder("<a href=\"");
            if (end != -1) { // if the end of the url is not the end of the
                             // token
                modify.append(current.substring(0, end).replaceAll("&amp;", "&"));
                modify.append("\">");
                modify.append(current.substring(0, end));
                modify.append("</a>");
                modify.append(current.substring(end));
            } else { // if the end of the url is the end of the token
                modify.append(current.substring(0).replaceAll("&amp;", "&"));
                modify.append("\">");
                modify.append(current.substring(0));
                modify.append("</a>");
            }
            current = modify.toString();
        }
        result.append(current);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("htmlifyText() - returning: " + result);
    }

    return result.toString();
}

From source file:com.haulmont.cuba.core.global.QueryTransformerRegex.java

@Override
public void removeOrderBy() {
    Matcher matcher = ORDER_BY_PATTERN.matcher(buffer);
    if (matcher.find()) {
        buffer.delete(matcher.start(), buffer.length());
    }/*from  w  w  w . ja v  a2  s. c o  m*/
}

From source file:fr.paris.lutece.plugins.stock.modules.billetterie.web.AbstractJspBean.java

/**
 * Ensure path given which use indexing value like [%d] is cleaning
 *
 * @param propertyPath/*from   w  w w.ja  v  a2 s.co  m*/
 *            the property path to clean
 * @return the right i18n key
 */
private String correctPath(Path propertyPath) {
    String path = propertyPath.toString();

    Pattern pattern = Pattern.compile("\\[\\d*\\]");
    Matcher matcher = pattern.matcher(path);

    if (matcher.find()) {
        path = path.substring(0, matcher.start())
                + path.substring(matcher.start() + matcher.group().length(), path.length());
    }

    return path;
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropsCompletionProvider.java

@Override
public CompletionTask createTask(int queryType, JTextComponent jtc) {
    if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) {
        return null;
    }// w  w w. j a v a  2  s.c om
    Project prj = Utilities.actionsGlobalContext().lookup(Project.class);
    if (prj == null) {
        return null;
    }
    logger.log(FINE, "Completing within context of prj {0}",
            FileUtil.getFileDisplayName(prj.getProjectDirectory()));
    final SpringBootService sbs = prj.getLookup().lookup(SpringBootService.class);
    if (sbs == null) {
        return null;
    }
    logger.fine("Creating completion task");
    return new AsyncCompletionTask(new AsyncCompletionQuery() {
        @Override
        protected void query(CompletionResultSet completionResultSet, Document document, int caretOffset) {
            final StyledDocument styDoc = (StyledDocument) document;
            Element lineElement = styDoc.getParagraphElement(caretOffset);
            int lineStartOffset = lineElement.getStartOffset();
            try {
                String lineToCaret = styDoc.getText(lineStartOffset, caretOffset - lineStartOffset);
                logger.log(FINER, "Completion on line to caret: {0}", lineToCaret);
                if (!lineToCaret.contains("#")) {
                    String[] parts = lineToCaret.split("=");
                    //property name extraction from part before =
                    Matcher matcher = PATTERN_PROP_NAME.matcher(parts[0]);
                    String propPrefix = null;
                    int propPrefixOffset = 0;
                    while (matcher.find()) {
                        propPrefix = matcher.group();
                        propPrefixOffset = matcher.start();
                    }
                    // check which kind of completion
                    final int equalSignOffset = lineToCaret.indexOf('=');
                    if (parts.length > 1) {
                        //value completion
                        String valPrefix = parts[1].trim();
                        completePropValue(sbs, completionResultSet, propPrefix, valPrefix,
                                lineStartOffset + lineToCaret.indexOf(valPrefix, equalSignOffset), caretOffset);
                    } else if (equalSignOffset >= 0) {
                        //value completion with empty filter
                        completePropValue(sbs, completionResultSet, propPrefix, null,
                                lineStartOffset + equalSignOffset + 1, caretOffset);
                    } else {
                        // property completion
                        completePropName(sbs, completionResultSet, propPrefix,
                                lineStartOffset + propPrefixOffset, caretOffset);
                    }
                }
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
            completionResultSet.finish();
        }
    }, jtc);
}

From source file:guru.nidi.languager.check.LinkChecker.java

public List<FindResult<String>> findBrokenLinks() {
    ExecutorService executor = Executors.newCachedThreadPool();
    final List<FindResult<String>> res = Collections.synchronizedList(new ArrayList<FindResult<String>>());
    final Set<String> urls = new HashSet<>();
    int lineNum = 1;

    for (MessageLine line : contents.subList(1, contents.size())) {
        lineNum++;//from  www  .ja va 2 s. c o m
        int col = 1;
        int elemNum = 0;
        for (String element : line) {
            final Matcher matcher = LINK_PATTERN.matcher(element);
            while (matcher.find()) {
                final String url = matcher.group();
                if (!urls.contains(url)) {
                    urls.add(url);
                    executor.submit(new LinkValidator(res, url,
                            new SourcePosition(file, lineNum, col + elemNum + matcher.start())));
                }
            }
            elemNum++;
            col += element.length();
        }
    }
    executor.shutdown();
    try {
        executor.awaitTermination(20, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        //ignore
    }
    return res;
}

From source file:com.haulmont.cuba.core.global.QueryTransformerRegex.java

@Override
public boolean removeDistinct() {
    Matcher matcher = SELECT_DISTINCT_PATTERN.matcher(buffer);
    if (matcher.find()) {
        buffer.replace(matcher.start(), matcher.end(), "select");
        return true;
    }//from www .  j a  v a  2  s  . c  o m
    return false;
}

From source file:com.epam.reportportal.extension.bugtracking.jira.JiraStrategy.java

/**
 * Parse ticket description and find binary data
 *
 * @param issueInput//from  www .jav a2s  .  c  o m
 * @return
 */
private Map<String, String> findBinaryData(IssueInput issueInput) {
    Map<String, String> binary = new HashMap<>();
    String description = issueInput.getField(IssueFieldId.DESCRIPTION_FIELD.id).getValue().toString();
    if (null != description) {
        // !54086a2c3c0c7d4446beb3e6.jpg| or [^54086a2c3c0c7d4446beb3e6.xml]
        String regex = "(!|\\[\\^).{24}.{0,5}(\\||\\])";
        Matcher matcher = Pattern.compile(regex).matcher(description);
        while (matcher.find()) {
            String rawValue = description.subSequence(matcher.start(), matcher.end()).toString();
            String binaryDataName = rawValue.replace("!", "").replace("[", "").replace("]", "").replace("^", "")
                    .replace("|", "");
            String binaryDataId = binaryDataName.split("\\.")[0];
            binary.put(binaryDataId, binaryDataName);
        }
    }
    return binary;
}

From source file:babel.content.pages.PageVersion.java

/**
 * Used to construct a page version from a set of nutch page related objects.
 * Should only be used by NutchPageExtractor.
 *///from w  w w  .  j  a  v a 2  s . c  om
public PageVersion(String segmentId, List<NutchChunk> chunks, Page page) {
    this();

    Writable curVal;
    CrawlDatum curCD;
    Content curCT;
    ParseData curPD;
    ParseText curPT;

    // Store Segment ID
    m_verProps.set(PROP_SEGMENT_ID, segmentId);

    // Unwrap all of the page related information
    for (NutchChunk chunk : chunks) {
        curVal = chunk.get();

        if (curVal instanceof CrawlDatum) {
            // Get fetch information
            curCD = (CrawlDatum) curVal;

            if (curCD.getStatus() == CrawlDatum.STATUS_FETCH_SUCCESS) {
                m_verProps.set(PROP_FETCH_TIME, Long.toString(curCD.getFetchTime()));
                m_verProps.set(PROP_MODIFIED_TIME, Long.toString(curCD.getModifiedTime()));
            }
        } else if (curVal instanceof Content) {
            // Get the original unparsed content
            curCT = (Content) curVal;

            try {
                String str = new String(curCT.getContent(), DEFAULT_CHARSET);
                Matcher m = Pattern.compile("<html[^>]*>", Pattern.CASE_INSENSITIVE).matcher(str);

                if (m.find()) {
                    str = str.substring(m.start(), m.end());
                    m = Pattern.compile("\\slang\\s*=\\s*\"*([^\\s=\"]+)\"*", Pattern.CASE_INSENSITIVE)
                            .matcher(str);

                    if (m.find()) {
                        str = str.substring(m.start(1), m.end(1)).trim().toLowerCase();
                        if (str.length() > 0) {
                            page.setLanguage(Language.fromString(str));
                        }
                    }
                }
            } catch (Exception e) {
            }
        } else if (curVal instanceof ParseData) {
            // Get data extracted from page content
            curPD = (ParseData) curVal;
            if (curPD.getStatus().isSuccess()) {
                m_verProps.set(PROP_TITLE, curPD.getTitle());
                m_parseMeta.setAll(curPD.getParseMeta());
                m_contentMeta.setAll(curPD.getContentMeta());
                m_outLinks = curPD.getOutlinks();
            }
        } else if (curVal instanceof ParseText) {
            // Get parsed content
            curPT = (ParseText) curVal;
            m_content = setStr(curPT.getText());
        } else if (LOG.isWarnEnabled()) {
            LOG.warn("Unrecognized type: " + curVal.getClass());
        }
    }
}

From source file:com.hp.alm.ali.idea.entity.tree.EntityNode.java

public String toString() {
    String name = entity.getPropertyValue("name");
    if (loading) {
        name += "..."; // TODO: show as icon
    }/*from www.j  a v  a 2s .c  om*/
    String filter = model.getFilter();
    if (!filter.isEmpty()) {
        Matcher matcher = Pattern.compile(wildcardToRegex(filter), Pattern.CASE_INSENSITIVE).matcher(name);
        List<Pair<Integer, Integer>> list = new LinkedList<Pair<Integer, Integer>>();
        while (matcher.find()) {
            list.add(new Pair<Integer, Integer>(matcher.start(), matcher.end()));
        }
        if (!list.isEmpty()) {
            Collections.reverse(list);
            for (Pair<Integer, Integer> match : list) {
                name = name.substring(0, match.first) + "<b>" + name.substring(match.first, match.second)
                        + "</b>" + name.substring(match.second);
            }
            return "<html>" + name + "</html>";
        }
    }
    return name;
}

From source file:com.cotrino.knowledgemap.db.Question.java

private void generateQuestion(Page page, List<String> sentences) {

    int firstSentence = (int) Math.round(Math.random() * (sentences.size() - QUESTION_SENTENCES));
    String question = "";
    for (int i = firstSentence; i < firstSentence + QUESTION_SENTENCES; i++) {
        question += sentences.get(i);/*  ww  w . j  a v a  2  s  .  com*/
    }
    Matcher matcher = LINK_PATTERN.matcher(question);
    List<Integer> matchStart = new LinkedList<Integer>();
    List<Integer> matchEnd = new LinkedList<Integer>();
    List<String> matchLink = new LinkedList<String>();
    while (matcher.find()) {
        String link = matcher.group(1);
        if (!link.contains(":")) {
            matchStart.add(matcher.start());
            matchEnd.add(matcher.end());
            matchLink.add(link);
        }
    }
    if (matchLink.size() > 0) {
        int linkToBeReplaced = (int) Math.round(Math.random() * (matchLink.size() - 1));
        int start = matchStart.get(linkToBeReplaced);
        int end = matchEnd.get(linkToBeReplaced);
        String link = matchLink.get(linkToBeReplaced);
        String[] answers = link.split("\\|");
        for (String answer : answers) {
            this.answers.add(answer);
        }
        String placeholder = link.replaceAll("[^ \\|]", ".");
        if (placeholder.contains("|")) {
            placeholder = placeholder.split("\\|")[1];
        }
        String text = question.substring(0, start) + "<span class='placeholder'>" + placeholder + "</span>"
                + question.substring(end);
        // replace other links of the kind [[A|B]] with A 
        text = text.replaceAll("\\[\\[[^\\]\\|]+\\|", "");
        // replace other links of the kind [[A]] with A
        text = text.replaceAll("\\[\\[", "").replaceAll("\\]\\]", "");
        //System.out.println("Question: "+text);
        this.question = text;
        this.page = page;
    }

}