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.spend.spendService.DomainLearning.java

private void GetNewQuery() {
    try {// w w w.  j av a2s.c  o m
        TimerTask timertask = new TimerTask() {
            public void run() {
                try {
                    domainList = new ArrayList<String>();
                    String[] seList = getSearchEngineNamesArray();
                    /* get urls from seedurlraw table */
                    PreparedStatement psmt = con.prepareStatement("SELECT url FROM seedurlraw");
                    ResultSet rs = psmt.executeQuery();
                    String regex = "[/]";
                    String regex2 = "[.]";
                    String PLDomain;
                    while (rs.next()) {
                        PLDomain = rs.getString("url");
                        PLDomain = PLDomain.replaceAll("http://|https://", "");
                        Pattern p = Pattern.compile(regex);
                        Matcher m = p.matcher(PLDomain);
                        if (m.find()) {
                            PLDomain = PLDomain.substring(0, m.start());
                        }
                        Pattern p2 = Pattern.compile(regex2);
                        Matcher m2 = p2.matcher(PLDomain);
                        int count = 0;
                        while (m2.find()) {
                            count++;
                        }
                        m2 = p2.matcher(PLDomain);
                        if (count > 1 && m2.find()) {
                            PLDomain = PLDomain.substring(m2.end());
                        }

                        //System.out.println(PLDomain);                        

                        if (!domainList.contains(PLDomain)) {
                            domainList.add(PLDomain);
                            newQuery = "sparql endpoint site:" + PLDomain;
                            for (Object se : seList) {
                                PreparedStatement psmt1 = con.prepareStatement(
                                        "INSERT INTO searchqueue(searchText,disabled,searchEngineName) VALUES(?,0,?);");
                                psmt1.setString(1, newQuery);
                                psmt1.setString(2, se.toString());
                                psmt1.executeUpdate();
                                psmt1.close();
                            }
                        }
                    }
                } catch (Exception ex) {
                    System.out
                            .println("DomainLearning.java timertask run function SQL ERROR " + ex.getMessage());
                }
            }
        };
        Timer timer = new Timer();
        DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        Date date = dateformat.parse("20-07-2017 00:00:00"); // set date and time
        timer.schedule(timertask, date, 1000 * 60 * 60 * 24 * 7); // for a week 1000*60*60*24*7
    } catch (Exception ex) {
        System.out.println("DomainLearning.java GetNewQuery function ERROR " + ex.getMessage());
    }
}

From source file:de.csw.ontology.XWikiTextEnhancer.java

/**
 * Extract from text all phrases that are enclosed by '[' and ']' denoting
 * an xWiki link./*from  ww w . j  a  v a  2 s. c om*/
 * 
 * @param text
 *            text to parse
 */
protected void initializeLinkIndex(String text) {
    if (text == null)
        throw new NullPointerException("Parameter text must not be null");

    linkIndex.clear();

    if (text.isEmpty())
        return;

    for (Pattern pattern : EXCLUDE_FROM_ENHANCEMENTS) {
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            linkIndex.put(matcher.start(), matcher.end());
        }
    }
}

From source file:com.joliciel.talismane.tokeniser.TokenSequenceImpl.java

private void initialise(String text, Pattern separatorPattern, Set<TokenPlaceholder> placeholders) {
    Matcher matcher = separatorPattern.matcher(text);
    Set<Integer> separatorMatches = new HashSet<Integer>();
    while (matcher.find())
        separatorMatches.add(matcher.start());

    Map<Integer, TokenPlaceholder> placeholderMap = new HashMap<Integer, TokenPlaceholder>();
    if (placeholders != null) {
        for (TokenPlaceholder placeholder : placeholders) {
            // take the first placeholder at this start index only
            // thus declaration order is the order at which they're applied
            if (!placeholderMap.containsKey(placeholder.getStartIndex()))
                placeholderMap.put(placeholder.getStartIndex(), placeholder);
        }// www  .  j a v a  2s  .  c  o  m
    }

    int currentPos = 0;
    for (int i = 0; i < text.length(); i++) {
        if (placeholderMap.containsKey(i)) {
            if (i > currentPos)
                this.addToken(currentPos, i);
            TokenPlaceholder placeholder = placeholderMap.get(i);
            Token token = this.addToken(placeholder.getStartIndex(), placeholder.getEndIndex());
            if (placeholder.getReplacement() != null)
                token.setText(placeholder.getReplacement());

            if (separatorPattern.matcher(token.getText()).matches()) {
                token.setSeparator(true);
            }

            // skip until after the placeholder
            i = placeholder.getEndIndex() - 1;
            currentPos = placeholder.getEndIndex();
        } else if (separatorMatches.contains(i)) {
            if (i > currentPos)
                this.addToken(currentPos, i);
            Token separator = this.addToken(i, i + 1);
            separator.setSeparator(true);
            currentPos = i + 1;
        }
    }

    if (currentPos < text.length())
        this.addToken(currentPos, text.length());

    this.finalise();
}

From source file:com.reprezen.swagedit.editor.hyperlinks.PathParamHyperlinkDetector.java

@Override
protected IHyperlink[] doDetect(SwaggerDocument doc, ITextViewer viewer, HyperlinkInfo info,
        JsonPointer pointer) {/*from  w w w.j a  va2  s .  co  m*/
    // find selected parameter
    Matcher matcher = PARAMETER_PATTERN.matcher(info.text);
    String parameter = null;
    int start = 0, end = 0;
    while (matcher.find() && parameter == null) {
        if (matcher.start() <= info.column && matcher.end() >= info.column) {
            parameter = matcher.group(1);
            start = matcher.start();
            end = matcher.end();
        }
    }

    // no parameter found
    if (emptyToNull(parameter) == null) {
        return null;
    }

    Iterable<JsonPointer> targetPaths = findParameterPath(doc, pointer, parameter);

    IRegion linkRegion = new Region(info.getOffset() + start, end - start);
    List<IHyperlink> links = new ArrayList<>();
    for (JsonPointer path : targetPaths) {
        IRegion target = doc.getRegion(path);

        if (target != null) {
            links.add(new SwaggerHyperlink(parameter, viewer, linkRegion, target));
        }
    }

    return links.isEmpty() ? null : links.toArray(new IHyperlink[links.size()]);
}

From source file:com.thoughtworks.go.domain.DefaultCommentRenderer.java

public String render(String text) {
    if (StringUtils.isBlank(text)) {
        return "";
    }//  w  w  w. jav  a  2 s . c  o  m
    if (regex.isEmpty() || link.isEmpty()) {
        Comment comment = new Comment();
        comment.escapeAndAdd(text);
        return comment.render();
    }
    try {
        Matcher matcher = Pattern.compile(regex).matcher(text);
        int start = 0;
        Comment comment = new Comment();
        while (hasMatch(matcher)) {
            comment.escapeAndAdd(text.substring(start, matcher.start()));
            comment.add(dynamicLink(matcher));
            start = matcher.end();
        }
        comment.escapeAndAdd(text.substring(start));
        return comment.render();
    } catch (PatternSyntaxException e) {
        LOGGER.warn("Illegal regular expression: {} - {}", regex, e.getMessage());
    }
    return text;
}

From source file:net.sf.jabref.gui.fieldeditors.JTextAreaWithHighlighting.java

/**
 * Highlight words in the Textarea/*from w w w.  j a va  2  s  . c  o m*/
 *
 * @param words to highlight
 */
private void highLight() {
    // highlight all characters that appear in charsToHighlight
    Highlighter highlighter = getHighlighter();
    highlighter.removeAllHighlights();

    if ((highlightPattern == null) || !highlightPattern.isPresent()) {
        return;
    }
    String content = getText();
    if (content.isEmpty()) {
        return;
    }

    highlightPattern.ifPresent(pattern -> {
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            try {
                highlighter.addHighlight(matcher.start(), matcher.end(), DefaultHighlighter.DefaultPainter);
            } catch (BadLocationException ble) {
                // should not occur if matcher works right
                LOGGER.warn("Highlighting not possible, bad location", ble);
            }
        }
    });

}

From source file:de.micromata.genome.gwiki.plugin.keywordsmarttags_1_0.GWikiFragmentKeywordHtml.java

protected void patch(KeyWordRanges kranges, String html,
        Map.Entry<String, Pair<Pattern, List<GWikiElementInfo>>> me) {
    if (StringUtils.isBlank(html) == true) {
        return;//from w  ww.java  2s . c om
    }
    Matcher m = me.getValue().getFirst().matcher(html);
    while (m.find() == true) {
        int sidx = m.start();
        int eidx = m.end();
        if (sidx != 0) {
            if (Character.isLetterOrDigit(html.charAt(sidx - 1)) == true) {
                continue;
            }
        }
        if (eidx < html.length() - 1) {
            if (Character.isLetterOrDigit(html.charAt(eidx)) == true) {
                continue;
            }
        }
        kranges.addIntersect(sidx, eidx, me.getValue().getSecond());
    }
}

From source file:org.ops4j.pax.web.itest.karaf.WebJSFKarafTest.java

@Test
public void testJSF() throws Exception {

    LOG.info("Testing JSF workflow!");
    String response = testClient.testWebPath("http://127.0.0.1:8181/war-jsf-sample", "Please enter your name");

    LOG.info("Found JSF starting page: {}", response);
    int indexOf = response.indexOf("id=\"javax.faces.ViewState\" value=");
    String substring = response.substring(indexOf + 34);
    indexOf = substring.indexOf("\"");
    substring = substring.substring(0, indexOf);

    Pattern pattern = Pattern.compile("(input id=\"mainForm:j_id_\\w*)");
    Matcher matcher = pattern.matcher(response);
    if (!matcher.find())
        fail("Didn't find required input id!");

    String inputID = response.substring(matcher.start(), matcher.end());
    inputID = inputID.substring(inputID.indexOf('"') + 1);
    LOG.info("Found ID: {}", inputID);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("mainForm:name", "Dummy-User"));

    nameValuePairs.add(new BasicNameValuePair("javax.faces.ViewState", substring.trim()));
    nameValuePairs.add(new BasicNameValuePair(inputID, "Press me"));
    nameValuePairs.add(new BasicNameValuePair("mainForm_SUBMIT", "1"));

    LOG.info("Will send the following NameValuePairs: {}", nameValuePairs);

    testClient.testPost("http://127.0.0.1:8181/war-jsf-sample/faces/helloWorld.jsp", nameValuePairs,
            "Hello Dummy-User. We hope you enjoy Apache MyFaces", 200);

}

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

private String writeScript(File file) throws IOException {
    StringBuffer sb = new StringBuffer(FileUtils.readFileToString(file, "utf8"));
    StringBuffer sb2 = new StringBuffer();
    for (;;) {// w w  w.j  av a  2  s.com
        Matcher m1 = javascriptStart.matcher(sb.toString());
        Matcher m2 = javascriptEnd.matcher(sb.toString());
        if (m1.find() && m2.find()) {
            sb2.append(sb.substring(m1.start(), m2.end()) + "\n\n");
            sb.delete(m1.start(), m2.end());
        } else {
            break;
        }
    }
    return sb2.toString();
}

From source file:io.cloudslang.lang.enforcer.SpringCleansingRule.java

private void findMatchesUsingPattern(final File javaFile, final Log log, final String contents,
        final Pattern pattern, String message) throws EnforcerRuleException {
    Matcher matcherImportForFile = pattern.matcher(contents);
    if (matcherImportForFile.find()) {
        int lineNumber = findLineNumber(contents.substring(0, matcherImportForFile.start()));
        log.info(format(FOUND_UNWANTED_OCCURRENCE_OF_ORG_SPRINGFRAMEWORK, lineNumber,
                javaFile.getAbsolutePath()));
        if (shouldFail) {
            throw new EnforcerRuleException(
                    message + valueOf(lineNumber) + IN_SOURCE + javaFile.getAbsolutePath() + "'");
        }/*  w w  w.j  ava 2s  . c  o  m*/
    }
}