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:org.ops4j.pax.web.itest.tomcat.WarJSFTCIntegrationTest.java

@Test
public void testJSF() throws Exception {

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

    LOG.debug("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.debug("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.debug("Will send the following NameValuePairs: {}", nameValuePairs);

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

}

From source file:disko.flow.analyzers.RegexpAnalyzer.java

private void processText(AnalysisContext<TextDocument> context, Ports ports, String docText, int offset) {
    String cleanText = cleanText(docText);
    for (Map.Entry<String, Pattern> e : patterns.entrySet()) {
        String tag = e.getKey();/*from   ww  w  .ja  va  2s .c om*/
        Pattern p = e.getValue();
        Matcher m = p.matcher(cleanText.replaceAll("[\\n\\r]", " "));
        log.debug("Looking for " + tag);
        while (m.find()) {
            final String found = m.group();
            final int start = m.start() + offset;
            final int end = m.end() + offset;
            DocumentAnn ann = new DocumentAnn(start, end, tag);
            log.debug("Found " + ann + ": " + found);
            if (saveAnnotations) {
                context.add(ann);
            }
        }
    }
}

From source file:com.norconex.importer.handler.transformer.impl.StripBeforeTransformer.java

@Override
protected void transformStringContent(String reference, StringBuilder content, ImporterMetadata metadata,
        boolean parsed, boolean partialContent) {
    if (stripBeforeRegex == null) {
        LOG.error("No regular expression provided.");
        return;// w  ww. j  a  va  2 s.  c o  m
    }
    int flags = Pattern.DOTALL | Pattern.UNICODE_CASE;
    if (!caseSensitive) {
        flags = flags | Pattern.CASE_INSENSITIVE;
    }
    Pattern pattern = Pattern.compile(stripBeforeRegex, flags);
    Matcher match = pattern.matcher(content);
    if (match.find()) {
        if (inclusive) {
            content.delete(0, match.end());
        } else {
            content.delete(0, match.start());
        }
    }
}

From source file:com.cloudbees.maven.release.policy.impl.CloudBeesVersionPolicy.java

/**
 * {@inheritDoc}// w  ww  .  ja v a  2 s  .  c o  m
 */
public VersionPolicyResult getDevelopmentVersion(VersionPolicyRequest versionPolicyRequest)
        throws PolicyException, VersionParseException {
    // this one is harder, we basically want to find the last incrementally updatable segment and bump it
    final VersionPolicyResult result = new VersionPolicyResult();
    String version = versionPolicyRequest.getVersion();
    if (version.endsWith("-SNAPSHOT")) {
        // if it is a -SNAPSHOT already, we shouldn't have been called, so we leave the value as is
        result.setVersion(version);
    } else {
        // find the first version format string (using "-" as separator)
        String[] comps = version.split("-");
        String externalRef = "";
        if (comps.length > 1 && !(version.contains("alpha") || version.contains("ALPHA")
                || version.contains("beta") || version.contains("BETA"))) {
            version = comps[0];
            for (int i = 1; i < comps.length; i++) {
                externalRef += "-" + comps[i];
            }
        }
        // find the last numeric segment and increment that
        Pattern separator = Pattern.compile("([\\.-])");
        Matcher m = separator.matcher(version);
        List<Integer> offsets = new ArrayList<Integer>();
        offsets.add(0);
        while (m.find()) {
            if (m.start() > 0) {
                offsets.add(m.start());
                offsets.add(m.start() + 1);
            }
        }
        offsets.add(version.length());
        boolean matched = false;
        for (int i = offsets.size() - 2; i >= 0; i -= 2) {
            final String segment = version.substring(offsets.get(i), offsets.get(i + 1));
            if (segment.matches("\\d+")) {
                result.setVersion(version.substring(0, offsets.get(i))
                        + new BigInteger(segment).add(BigInteger.ONE).toString()
                        + version.substring(offsets.get(i + 1)) + externalRef + "-SNAPSHOT");
                matched = true;
                break;
            }
            // we will handle -ALPHA -> BETA-1 and .ALPHA -> .BETA.1 (note we add a segment)
            if ("alpha".equals(segment)) {
                // from the regex it is either a '.' or a '-' so charAt is safe from multi-word code points
                char separatorChar = (offsets.get(i) > 0 ? version.charAt(offsets.get(i) - 1) : '-');
                result.setVersion(version.substring(0, offsets.get(i)) + "beta" + separatorChar + "1"
                        + version.substring(offsets.get(i + 1)) + "-SNAPSHOT");
                matched = true;
                break;
            }
            if ("ALPHA".equals(segment)) {
                // from the regex it is either a '.' or a '-' so charAt is safe from multi-word code points
                char separatorChar = (offsets.get(i) > 0 ? version.charAt(offsets.get(i) - 1) : '-');
                result.setVersion(version.substring(0, offsets.get(i)) + "BETA" + separatorChar + "1"
                        + version.substring(offsets.get(i + 1)) + "-SNAPSHOT");
                matched = true;
                break;
            }
            if ("beta".equals(segment)) {
                // from the regex it is either a '.' or a '-' so charAt is safe from multi-word code points
                char separatorChar = (offsets.get(i) > 0 ? version.charAt(offsets.get(i) - 1) : '-');
                result.setVersion(version.substring(0, offsets.get(i)) + "rc" + separatorChar + "1"
                        + version.substring(offsets.get(i + 1)) + "-SNAPSHOT");
                matched = true;
                break;
            }
            if ("BETA".equals(segment)) {
                // from the regex it is either a '.' or a '-' so charAt is safe from multi-word code points
                char separatorChar = (offsets.get(i) > 0 ? version.charAt(offsets.get(i) - 1) : '-');
                result.setVersion(version.substring(0, offsets.get(i)) + "RC" + separatorChar + "1"
                        + version.substring(offsets.get(i + 1)) + "-SNAPSHOT");
                matched = true;
                break;
            }
        }
        if (!matched) {
            throw new VersionParseException("Unsupported version number format: " + version);
        }
    }
    return result;
}

From source file:com.flexive.war.filter.BrowserDetect.java

/**
 * Return the browser version, if available.
 *
 * @return  the browser version, if available.
 * @since 3.1/* ww w  . j a  v  a2  s  .  c  o  m*/
 */
public double getBrowserVersion() {
    if (browserVersion < 0) {
        final String versionString;
        final Browser browser = getBrowser();
        if (BROWSER_VERSION_OVERRIDES.containsKey(browser)) {
            versionString = BROWSER_VERSION_OVERRIDES.get(browser);
        } else {
            versionString = BROWSER_IDS.get(browser);
        }
        final int pos = ua.indexOf(versionString);
        browserVersion = DEFAULT_VERSION;
        if (pos != -1) {
            final Matcher matcher = P_VERSION.matcher(ua);
            if (matcher.find(pos + versionString.length())) {
                try {
                    browserVersion = Double.parseDouble(ua.substring(matcher.start(), matcher.end()));
                } catch (NumberFormatException e) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Failed to extract browser version from user agent: '" + ua + "'");
                    }
                    browserVersion = DEFAULT_VERSION;
                }
            }
        }
    }
    return browserVersion;
}

From source file:com.norconex.importer.handler.tagger.impl.TextStatisticsTagger.java

@Override
protected void tagTextDocument(String reference, Reader input, ImporterMetadata metadata, boolean parsed)
        throws ImporterHandlerException {
    long charCount = 0;
    long wordCharCount = 0;
    long wordCount = 0;
    long sentenceCount = 0;
    long sentenceCharCount = 0;
    long paragraphCount = 0;

    //TODO make this more efficient, by doing all this in one pass.
    LineIterator it = IOUtils.lineIterator(input);
    while (it.hasNext()) {
        String line = it.nextLine().trim();
        if (StringUtils.isBlank(line)) {
            continue;
        }//  w ww.j  a v a 2s .  c o m

        // Paragraph
        paragraphCount++;

        // Character
        charCount += line.length();

        // Word
        Matcher matcher = PATTERN_WORD.matcher(line);
        while (matcher.find()) {
            int wordLength = matcher.end() - matcher.start();
            wordCount++;
            wordCharCount += wordLength;
        }

        // Sentence
        BreakIterator boundary = BreakIterator.getSentenceInstance();
        boundary.setText(line);
        int start = boundary.first();
        for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
            sentenceCharCount += (end - start);
            sentenceCount++;
        }
    }

    String field = StringUtils.EMPTY;
    if (StringUtils.isNotBlank(fieldName)) {
        field = fieldName.trim() + ".";
    }

    //--- Add fields ---
    metadata.addLong("document.stat." + field + "characterCount", charCount);
    metadata.addLong("document.stat." + field + "wordCount", wordCount);
    metadata.addLong("document.stat." + field + "sentenceCount", sentenceCount);
    metadata.addLong("document.stat." + field + "paragraphCount", paragraphCount);
    metadata.addString("document.stat." + field + "averageWordCharacterCount",
            divide(wordCharCount, wordCount));
    metadata.addString("document.stat." + field + "averageSentenceCharacterCount",
            divide(sentenceCharCount, sentenceCount));
    metadata.addString("document.stat." + field + "averageSentenceWordCount", divide(wordCount, sentenceCount));
    metadata.addString("document.stat." + field + "averageParagraphCharacterCount",
            divide(charCount, paragraphCount));
    metadata.addString("document.stat." + field + "averageParagraphSentenceCount",
            divide(sentenceCount, paragraphCount));
    metadata.addString("document.stat." + field + "averageParagraphWordCount",
            divide(wordCount, paragraphCount));

}

From source file:net.minecraftforge.common.ForgeHooks.java

public static ITextComponent newChatWithLinks(String string, boolean allowMissingHeader) {
    // Includes ipv4 and domain pattern
    // Matches an ip (xx.xxx.xx.xxx) or a domain (something.com) with or
    // without a protocol or path.
    ITextComponent ichat = null;//from  w w  w . j  av a2s. c o m
    Matcher matcher = URL_PATTERN.matcher(string);
    int lastEnd = 0;

    // Find all urls
    while (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();

        // Append the previous left overs.
        String part = string.substring(lastEnd, start);
        if (part.length() > 0) {
            if (ichat == null)
                ichat = new TextComponentString(part);
            else
                ichat.appendText(part);
        }
        lastEnd = end;
        String url = string.substring(start, end);
        ITextComponent link = new TextComponentString(url);

        try {
            // Add schema so client doesn't crash.
            if ((new URI(url)).getScheme() == null) {
                if (!allowMissingHeader) {
                    if (ichat == null)
                        ichat = new TextComponentString(url);
                    else
                        ichat.appendText(url);
                    continue;
                }
                url = "http://" + url;
            }
        } catch (URISyntaxException e) {
            // Bad syntax bail out!
            if (ichat == null)
                ichat = new TextComponentString(url);
            else
                ichat.appendText(url);
            continue;
        }

        // Set the click event and append the link.
        ClickEvent click = new ClickEvent(ClickEvent.Action.OPEN_URL, url);
        link.getStyle().setClickEvent(click);
        link.getStyle().setUnderlined(true);
        link.getStyle().setColor(TextFormatting.BLUE);
        if (ichat == null)
            ichat = link;
        else
            ichat.appendSibling(link);
    }

    // Append the rest of the message.
    String end = string.substring(lastEnd);
    if (ichat == null)
        ichat = new TextComponentString(end);
    else if (end.length() > 0)
        ichat.appendText(string.substring(lastEnd));
    return ichat;
}

From source file:com.legstar.pli2cob.AbstractPLIStructureCobolEmitter.java

/**
 * Inverts the repetition factors.//from   ww  w  .java  2  s  .  com
 * @param picture the PL/I picture
 * @return a string with repetition factors inverted
 * TODO scaling factors are missed for repetition factors
 */
protected String invertRepetitionFactor(final String picture) {
    StringBuilder sb = new StringBuilder();
    int current = 0;
    Matcher matcher = REPETITION_FACTOR_PATTERN.matcher(picture);
    while (matcher.find()) {
        sb.append(picture.substring(current, matcher.start()));
        sb.append(picture.charAt(matcher.end()));
        sb.append(matcher.group());
        current = matcher.end() + 1;
    }
    if (current < picture.length()) {
        sb.append(picture.substring(current));
    }
    return sb.toString();
}

From source file:net.java.sip.communicator.impl.gui.main.chat.replacers.KeywordReplacer.java

/**
 * Replace operation. Searches for the keyword in the provided piece of
 * content and replaces it with the piece of content surrounded by &lt;b&gt;
 * tags./*w  w  w  .ja  v  a  2  s .  c  om*/
 *
 * @param target the destination to write the result to
 * @param piece the piece of content to process
 */
@Override
public void replace(final StringBuilder target, final String piece) {
    if (this.keyword == null || this.keyword.isEmpty()) {
        target.append(StringEscapeUtils.escapeHtml4(piece));
        return;
    }

    final Matcher m = Pattern
            .compile("(^|\\W)(" + Pattern.quote(keyword) + ")(\\W|$)", Pattern.CASE_INSENSITIVE).matcher(piece);
    int prevEnd = 0;
    while (m.find()) {
        target.append(StringEscapeUtils.escapeHtml4(
                piece.substring(prevEnd, m.start() + m.group(INDEX_OPTIONAL_PREFIX_GROUP).length())));
        prevEnd = m.end() - m.group(INDEX_OPTIONAL_SUFFIX_GROUP).length();
        final String keywordMatch = m.group(INDEX_KEYWORD_MATCH_GROUP).trim();
        target.append("<b>");
        target.append(StringEscapeUtils.escapeHtml4(keywordMatch));
        target.append("</b>");
    }
    target.append(StringEscapeUtils.escapeHtml4(piece.substring(prevEnd)));
}

From source file:com.joliciel.talismane.sentenceDetector.SentenceDetectorEventStream.java

@Override
public boolean hasNext() {
    while (currentSentence == null) {
        currentIndex = 0;/*from www . j  a va  2s  . co m*/
        possibleBoundaries = new ArrayList<Integer>();
        if (!sentences.isEmpty()) {
            currentSentence = sentences.poll();
        } else if (corpusReader.hasNextSentence()) {
            currentSentence = corpusReader.nextSentence();
        } else {
            break;
        }
        if (currentSentence != null) {
            Matcher matcher = SentenceDetector.POSSIBLE_BOUNDARIES.matcher(currentSentence);

            while (matcher.find()) {
                possibleBoundaries.add(previousSentence.length() + matcher.start());
            }
            realBoundary = previousSentence.length() + currentSentence.length() - 1;
        }
        if (possibleBoundaries.size() == 0) {
            if (currentSentence.endsWith(" "))
                previousSentence = currentSentence;
            else
                previousSentence = currentSentence + " ";
            currentSentence = null;
        }
    }

    return (currentSentence != null);
}