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: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  ww w  .  j av a  2 s  . c  om
    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:eu.eexcess.sourceselection.redde.indexer.trec.topic.TrecTopicTokenizer.java

/**
 * Tokenize trec-topic xml file respecting missing closing xml tags.
 * @param input/* w  w  w .  ja  v a2 s .  c om*/
 * @return
 * @throws IOException
 */
public List<Topic> tokenize(InputStream input) throws IOException {
    Writer writer = new StringWriter();
    IOUtils.copy(input, writer);
    String data = writer.toString();

    Matcher startTopicMatcher = getStartTopicTagMatcher(data);
    Matcher endTopicTagMatcher = getEndTopicTagMatcher(data);

    int processedChars = 0;
    while (startTopicMatcher.find(processedChars)) {
        if (endTopicTagMatcher.find(startTopicMatcher.end())) {
            String topicData = data.substring(processedChars, endTopicTagMatcher.start());
            Matcher startTagMatcher = getStartGenericTagMatcher(topicData);

            while (startTagMatcher.find()) {
                Matcher endTagMatcher = getStartGenericTagMatcher(topicData);
                if (endTagMatcher.find(startTagMatcher.end())) {
                    assignValueToField(startTagMatcher.group(),
                            topicData.substring(startTagMatcher.end(), endTagMatcher.start()));
                } else {
                    // reached last tag in topic
                    assignValueToField(startTagMatcher.group(),
                            topicData.substring(startTagMatcher.end(), topicData.length()));
                }

            }

        } else {
            throw new IllegalArgumentException("expected topic end tag");
        }
        processedChars = endTopicTagMatcher.end();
        topics.add(currentTopic);
        currentTopic = new Topic();
    }
    return topics;
}

From source file:com.sangupta.pep.Generator.java

private SlideVariables getSlideVariables(String slideContents) {
    SlideVariables vars = new SlideVariables();

    Matcher matcher = PATTERN.matcher(slideContents);

    if (matcher != null && matcher.matches()) {
        vars.setHeader(matcher.group(1));
        vars.setLevel(Integer.valueOf(matcher.group(2)));
        vars.setTitle(matcher.group(3));
        vars.setContent(matcher.group(4));
    } else {/*from  w ww .  java 2s.  c  o  m*/
        vars.setHeader("");
        vars.setTitle("");
        vars.setContent(slideContents);
        vars.setLevel(0);
    }

    // process slide classes
    ContentAndClasses cc = processMacros(vars);

    String content = cc.getContent();
    vars.setContent(content);
    vars.setClasses(cc.getClasses().toArray(new String[0]));

    if (StringUtils.isNotEmpty(content)) {
        content = content.trim();
        Pattern p2 = Pattern.compile("<h\\d[^>]*>presenter notes</h\\d>",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);
        Matcher m2 = p2.matcher(content);

        if (m2 != null && m2.matches()) {
            vars.setPresenterNotes(content.substring(m2.end()).trim());
            content = content.substring(0, m2.start());

            vars.setContent(content);
        }
    }

    vars.setRelativeSourcePath(this.inputFile.getPath());
    vars.setAbsoluteSourcePath(this.inputFile.getAbsolutePath());

    return vars;
}

From source file:com.seajas.search.contender.service.modifier.ModifierFilterProcessor.java

/**
 * Process the given reader using this filter.
 * /*from w w w .ja  v a 2s .  c  om*/
 * @param filter
 * @param reader
 * @return Reader
 * @throws IOException
 */
public Reader process(final ModifierFilter filter, final Reader reader) throws IOException {
    StringBuffer stringBuffer = new StringBuffer();

    for (int c; (c = reader.read()) != -1;)
        stringBuffer.append((char) c);

    reader.close();

    if (!filter.getIsExpression()) {
        Pattern pattern = Pattern.compile(
                Pattern.quote(filter.getFragmentStart()) + ".*" + Pattern.quote(filter.getFragmentEnd()),
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

        return new StringReader(
                pattern.matcher(stringBuffer).replaceAll(filter.getFragmentStart() + filter.getFragmentEnd()));
    } else {
        Matcher startMatcher = Pattern.compile(filter.getFragmentStart(), Pattern.CASE_INSENSITIVE)
                .matcher(stringBuffer),
                endMatcher = Pattern.compile(filter.getFragmentEnd(), Pattern.CASE_INSENSITIVE)
                        .matcher(stringBuffer);

        while (startMatcher.find() && endMatcher.find(startMatcher.end()))
            if (startMatcher.end() != endMatcher.start()) {
                stringBuffer.delete(startMatcher.end(), endMatcher.start());

                startMatcher.reset();
                endMatcher.reset();
            }

        // Store the result

        return new StringReader(stringBuffer.toString());
    }
}

From source file:h2weibo.utils.StatusImageExtractor.java

public byte[] extract(StringBuffer input) {
    if (input == null)
        return null;

    for (String key : simplePatterns.keySet()) {
        Pattern p = Pattern.compile(key);
        Matcher m = p.matcher(input);

        if (m.find()) {
            String mediaUrl = simplePatterns.get(key);
            mediaUrl = mediaUrl.replaceAll("_KEY_", m.group(1));

            try {
                byte[] bytes = downloadUrl(mediaUrl);
                input.delete(m.start(), m.end()); // -1 for the space before
                return bytes;
            } catch (IOException e) {
                log.error("Not able to download image", e);
            }//from w w w .  j a  v a  2s  .c  o m
        }
    }

    for (String key : jsonPatterns.keySet()) {
        Pattern p = Pattern.compile(key);
        Matcher m = p.matcher(input);

        if (m.find()) {
            String jsonUrl = jsonPatterns.get(key)[0];
            jsonUrl = jsonUrl.replaceAll("_KEY_", m.group(1));

            try {
                byte[] jsonData = downloadUrl(jsonUrl);
                JSONObject obj = new JSONObject(new String(jsonData));
                String imageUrl = (String) obj.get(jsonPatterns.get(key)[1]);

                return downloadUrl(imageUrl);
            } catch (IOException e) {
                log.error("Not able to download image", e);
            } catch (JSONException e) {
                log.error("Not able to parse json", e);
            }
        }
    }

    return null;
}

From source file:alluxio.cli.fs.command.StatCommand.java

private String formatOutput(CommandLine cl, URIStatus status) {
    String format = cl.getOptionValue('f');
    int formatLen = format.length();

    StringBuilder output = new StringBuilder();
    Matcher m = FORMAT_PATTERN.matcher(format);
    int i = 0;//  w w w .  ja v  a2s.c  o m
    while (i < formatLen && m.find(i)) {
        if (m.start() != i) {
            output.append(format.substring(i, m.start()));
        }
        output.append(getField(m, status));
        i = m.end();
    }
    if (i < formatLen) {
        output.append(format.substring(i));
    }
    return output.toString();
}

From source file:us.pserver.revok.HttpConnector.java

/**
 * Set network address and port <code>&lt;address&gt;:&lt;port&gt;</code>.
 * @param addr <code>String</code>.
 * @return This modified <code>HttpConnector</code> instance.
 *//*  w  w  w.j a  va 2s  . c  o m*/
public HttpConnector setAddress(String addr) {
    if (addr == null)
        throw new IllegalArgumentException(
                "[HttpConnector.setAddress( String )] " + "Invalid address [" + addr + "]");

    Pattern pt = Pattern.compile("[\\w]+://");
    Matcher m = pt.matcher(addr);
    if (m.find()) {
        proto = addr.substring(m.start(), m.end());
        addr = addr.substring(m.end());
    }

    pt = Pattern.compile(":[\\d]+");
    m = pt.matcher(addr);
    if (m.find()) {
        port = Integer.parseInt(addr.substring(m.start() + 1, m.end()));
        if (port <= 0 || port > 65535)
            throw new IllegalArgumentException("Port out of range 1-65535 {" + port + "}");
        addr = addr.substring(0, m.start()).concat(addr.substring(m.end()));
    }

    pt = Pattern.compile("/[\\w].");
    m = pt.matcher(addr);
    if (m.find()) {
        path = addr.substring(m.start());
        addr = addr.substring(0, m.start());
    }

    address = addr;
    return this;
}

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

/**
 * Inverts the repetition factors./*from ww w . ja  v  a  2 s .  co  m*/
 * @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:com.squarespace.template.MapFormat.java

private void init(String raw) {
    StringBuffer buf = new StringBuffer();
    Matcher matcher = KEY_ESCAPE.matcher(raw);
    int mark = 0;
    int length = raw.length();
    while (matcher.find()) {
        int start0 = matcher.start();
        if (mark < start0) {
            buf.append(raw.substring(mark, start0));
        }/*  ww  w . j a  v  a2s  .  co m*/
        buf.append(raw.charAt(start0));
        keys.add(raw.substring(start0 + 2, matcher.end() - 1));
        mark = matcher.end();
    }
    if (mark < length) {
        buf.append(raw.substring(mark, length));
    }
    this.format = buf.toString();
}

From source file:de.fau.cs.osr.utils.StringUtils.java

public static String xmlDecode(String text, XmlEntityResolver resolver) {
    Pattern rx = XmlGrammar.xmlReference();

    int start = 0;
    StringBuilder b = new StringBuilder();
    while (true) {
        Matcher m = rx.matcher(text);
        if (m.find(start)) {
            b.append(text.substring(start, m.start()));

            String resolved = null;
            if (m.group(1) != null) {
                resolved = resolver.resolveXmlEntity(m.group(1));
            } else {
                try {
                    boolean decimal = m.group(2) != null;

                    String num = decimal ? m.group(2) : m.group(3);

                    int val = Integer.valueOf(num, decimal ? 10 : 16);
                    if (val >= 0x20 && val != 0x7F)
                        resolved = String.valueOf((char) val);
                } catch (NumberFormatException e) {
                }//w w  w .j a v  a  2  s . c o  m
            }

            if (resolved != null)
                b.append(resolved);
            else
                b.append(text.substring(m.start(), m.end()));

            start = m.end();
        } else {
            if (start < text.length())
                b.append(text.substring(start));

            break;
        }
    }
    return b.toString();
}