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(String name) 

Source Link

Document

Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:com.github.gekoh.yagen.hibernate.PatchHibernateMappingClasses.java

public static SqlStatement prepareDDL(String sql) {
    sql = sql.trim();/*  ww w.  j a  va 2s  .  c  o  m*/
    String delimiter = "";

    Matcher matcher = PLSQL_END_PATTERN.matcher(sql);
    if (matcher.find()) {
        if (matcher.group(2) != null) {
            sql = sql.substring(0, matcher.start(2));
        }
        sql += "\n";
        delimiter = "/";
    }
    // remove trailing semicolon in case of non pl/sql type objects/statements
    else if (sql.endsWith(";")) {
        sql = sql.substring(0, sql.length() - 1);
    }

    StringBuilder sqlWoComments = new StringBuilder(sql);
    while ((matcher = COMMENT_PATTERN.matcher(sqlWoComments.toString())).find()) {
        sqlWoComments.delete(matcher.start(), matcher.end());
    }

    if (delimiter.length() < 1 && sqlWoComments.toString().trim().length() > 0) {
        delimiter = ";";
    }

    return new SqlStatementImpl(sql, delimiter);
}

From source file:de.uniwue.info6.misc.StringTools.java

/**
 *
 *
 * @param queryText/*  w ww. j a  v a  2s.  co  m*/
 * @param tables
 * @param user
 * @return
 */
public static String addUserPrefix(String queryText, List<String> tables, User user) {
    LinkedList<Integer> substrings = new LinkedList<Integer>();
    int start = 0, end = queryText.length();
    for (String tab : tables) { // q&d fix
        Matcher matcher = Pattern.compile(tab.trim(), Pattern.CASE_INSENSITIVE).matcher(queryText);
        while (matcher.find()) {
            start = matcher.start(0);
            end = matcher.end(0);
            // String group = matcher.group();
            boolean leftCharacterValid = StringTools.trailingCharacter(queryText, start, true);
            boolean rightCharacterValid = StringTools.trailingCharacter(queryText, end, false);
            if (leftCharacterValid && rightCharacterValid) {
                substrings.add(start);
            }
        }
    }

    Collections.sort(substrings);

    for (int i = substrings.size() - 1; i >= 0; i--) {
        Integer sub = substrings.get(i);
        queryText = queryText.substring(0, sub) + user.getId() + "_"
                + queryText.substring(sub, queryText.length());
    }

    return queryText;
}

From source file:com.puppycrawl.tools.checkstyle.utils.JavadocUtils.java

/**
 * Looks for inline tags in comment and adds them to the proper tags collection.
 * @param comment comment text block//from  ww  w .j a  v a 2s.c  o  m
 * @param lineNumber line number in the comment
 * @param validTags collection of valid tags
 * @param invalidTags collection of invalid tags
 */
private static void lookForInlineTags(TextBlock comment, int lineNumber, final List<JavadocTag> validTags,
        final List<InvalidJavadocTag> invalidTags) {
    final String text = comment.getText()[lineNumber];
    // Match Javadoc text after comment characters
    final Pattern commentPattern = Pattern.compile("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
    final Matcher commentMatcher = commentPattern.matcher(text);
    final String commentContents;

    // offset including comment characters
    final int commentOffset;

    if (commentMatcher.find()) {
        commentContents = commentMatcher.group(1);
        commentOffset = commentMatcher.start(1) - 1;
    } else {
        // No leading asterisks, still valid
        commentContents = text;
        commentOffset = 0;
    }
    final Pattern tagPattern = Pattern.compile(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
    final Matcher tagMatcher = tagPattern.matcher(commentContents);
    while (tagMatcher.find()) {
        final String tagName = tagMatcher.group(1);
        final String tagValue = tagMatcher.group(2).trim();
        final int line = comment.getStartLineNo() + lineNumber;
        int col = commentOffset + tagMatcher.start(1) - 1;
        if (lineNumber == 0) {
            col += comment.getStartColNo();
        }
        if (JavadocTagInfo.isValidName(tagName)) {
            validTags.add(new JavadocTag(line, col, tagName, tagValue));
        } else {
            invalidTags.add(new InvalidJavadocTag(line, col, tagName));
        }
    }
}

From source file:it.unibo.alchemist.language.protelis.util.ProtelisLoader.java

private static void loadResourcesRecursively(final XtextResourceSet target, final String programURI,
        final Set<String> alreadyInQueue) throws IOException {
    final String realURI = (programURI.startsWith("/") ? "classpath:" : "") + programURI;
    if (!alreadyInQueue.contains(realURI)) {
        alreadyInQueue.add(realURI);//  www .  j  av  a 2 s.c  o m
        final URI uri = URI.createURI(realURI);
        final org.springframework.core.io.Resource protelisFile = RESOLVER.getResource(realURI);
        final InputStream is = protelisFile.getInputStream();
        final String ss = IOUtils.toString(is, "UTF-8");
        final Matcher matcher = REGEX_PROTELIS_IMPORT.matcher(ss);
        while (matcher.find()) {
            final int start = matcher.start(1);
            final int end = matcher.end(1);
            final String imp = ss.substring(start, end);
            final String classpathResource = "classpath:/" + imp.replace(":", "/") + "."
                    + PROTELIS_FILE_EXTENSION;
            loadResourcesRecursively(target, classpathResource, alreadyInQueue);
        }
        target.getResource(uri, true);
    }
}

From source file:org.protelis.lang.ProtelisLoader.java

private static void loadStringResources(final XtextResourceSet target, final InputStream is)
        throws IOException {
    final Set<String> alreadyInQueue = new LinkedHashSet<>();
    final String ss = IOUtils.toString(is, "UTF-8");
    final Matcher matcher = REGEX_PROTELIS_IMPORT.matcher(ss);
    while (matcher.find()) {
        final int start = matcher.start(1);
        final int end = matcher.end(1);
        final String imp = ss.substring(start, end);
        final String classpathResource = "classpath:/" + imp.replace(":", "/") + "." + PROTELIS_FILE_EXTENSION;
        loadResourcesRecursively(target, classpathResource, alreadyInQueue);
    }/*w w w. j a va 2s .c  o m*/
}

From source file:com.cubusmail.mail.text.MessageTextUtil.java

/**
 * Convert a plaint text to html./*from w  w  w  .j ava2 s.co  m*/
 * 
 * @param plainText
 * @return
 */
public static String convertPlainText2Html(String plainText, MessageTextMode mode) {

    try {
        plainText = HtmlUtils.htmlEscape(plainText).replaceAll(REPL_LINEBREAK, HTML_BR);

        final Matcher m = PATTERN_HREF.matcher(plainText);
        final StringBuffer sb = new StringBuffer(plainText.length());
        final StringBuilder tmp = new StringBuilder(256);
        while (m.find()) {
            final String nonHtmlLink = m.group(1);
            if ((nonHtmlLink == null) || (hasSrcAttribute(plainText, m.start(1)))) {
                m.appendReplacement(sb, Matcher.quoteReplacement(checkTarget(m.group())));
            } else {
                tmp.setLength(0);
                m.appendReplacement(sb, tmp.append("<a href=\"").append(
                        (nonHtmlLink.startsWith("www") || nonHtmlLink.startsWith("news") ? "http://" : ""))
                        .append("$1\" target=\"_blank\">$1</a>").toString());
            }
        }
        m.appendTail(sb);

        if (mode == MessageTextMode.DISPLAY) {
            sb.insert(0, "<p style=\"font-family: monospace; font-size: 10pt;\">");
            sb.append("</p>");
        }

        return sb.toString();
    } catch (final Exception e) {
        log.error(e.getMessage(), e);
    } catch (final StackOverflowError error) {
        log.error(StackOverflowError.class.getName(), error);
    }

    return plainText;
}

From source file:com.feilong.core.util.RegexUtil.java

/**
 * ??????./*from www . j av  a 2s  . com*/
 * 
 * <p>
 * ? m?? s  g,? m.group(g)  s.substring(m.start(g), m.end(g)).<br>
 * ? 1?.0?,? m.group(0) m.group().
 * </p>
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * String regexPattern = "(.*?)@(.*?)";
 * String email = "venusdrogon@163.com";
 * RegexUtil.group(regexPattern, email);
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 *    0 venusdrogon@163.com
 *    1 venusdrogon
 *    2 163.com
 * </pre>
 * 
 * </blockquote>
 * 
 * @param regexPattern
 *            ?,pls use {@link RegexPattern}
 * @param input
 *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
 * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
 *          <code>input</code> null, {@link NullPointerException}<br>
 *          ??, {@link java.util.Collections#emptyMap()}
 * @see #getMatcher(String, CharSequence)
 * @see Matcher#group(int)
 * @since 1.0.7
 */
public static Map<Integer, String> group(String regexPattern, CharSequence input) {
    Matcher matcher = getMatcher(regexPattern, input);
    if (!matcher.matches()) {
        LOGGER.trace("[not matches] ,\n\tregexPattern:[{}] \n\tinput:[{}]", regexPattern, input);
        return Collections.emptyMap();
    }
    int groupCount = matcher.groupCount();
    Map<Integer, String> map = newLinkedHashMap(groupCount + 1);
    for (int i = 0; i <= groupCount; ++i) {
        //?
        String groupValue = matcher.group(i); //map.put(0, matcher.group());// ? 1 ?.0?,? m.group(0)  m.group().
        LOGGER.trace("matcher group[{}],start-end:[{}-{}],groupValue:[{}]", i, matcher.start(i), matcher.end(i),
                groupValue);
        map.put(i, groupValue);//groupValue
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("regexPattern:[{}],input:[{}],groupMap:{}", regexPattern, input, JsonUtil.format(map));
    }
    return map;
}

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocUtils.java

/**
 * Gets validTags from a given piece of Javadoc.
 * @param cmt/*w w  w .jav a 2 s. c om*/
 *        the Javadoc comment to process.
 * @param tagType
 *        the type of validTags we're interested in
 * @return all standalone validTags from the given javadoc.
 */
public static JavadocTags getJavadocTags(TextBlock cmt, JavadocTagType tagType) {
    final String[] text = cmt.getText();
    final List<JavadocTag> tags = Lists.newArrayList();
    final List<InvalidJavadocTag> invalidTags = Lists.newArrayList();
    Pattern blockTagPattern = Pattern.compile("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
    for (int i = 0; i < text.length; i++) {
        final String s = text[i];
        final Matcher blockTagMatcher = blockTagPattern.matcher(s);
        if ((tagType == JavadocTagType.ALL || tagType == JavadocTagType.BLOCK) && blockTagMatcher.find()) {
            final String tagName = blockTagMatcher.group(1);
            String content = s.substring(blockTagMatcher.end(1));
            if (content.endsWith("*/")) {
                content = content.substring(0, content.length() - 2);
            }
            final int line = cmt.getStartLineNo() + i;
            int col = blockTagMatcher.start(1) - 1;
            if (i == 0) {
                col += cmt.getStartColNo();
            }
            if (JavadocTagInfo.isValidName(tagName)) {
                tags.add(new JavadocTag(line, col, tagName, content.trim()));
            } else {
                invalidTags.add(new InvalidJavadocTag(line, col, tagName));
            }
        }
        // No block tag, so look for inline validTags
        else if (tagType == JavadocTagType.ALL || tagType == JavadocTagType.INLINE) {
            // Match Javadoc text after comment characters
            final Pattern commentPattern = Pattern.compile("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
            final Matcher commentMatcher = commentPattern.matcher(s);
            final String commentContents;
            final int commentOffset; // offset including comment characters
            if (commentMatcher.find()) {
                commentContents = commentMatcher.group(1);
                commentOffset = commentMatcher.start(1) - 1;
            } else {
                commentContents = s; // No leading asterisks, still valid
                commentOffset = 0;
            }
            final Pattern tagPattern = Pattern.compile(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}");
            final Matcher tagMatcher = tagPattern.matcher(commentContents);
            while (tagMatcher.find()) {
                final String tagName = tagMatcher.group(1);
                final String tagValue = tagMatcher.group(2).trim();
                final int line = cmt.getStartLineNo() + i;
                int col = commentOffset + tagMatcher.start(1) - 1;
                if (i == 0) {
                    col += cmt.getStartColNo();
                }
                if (JavadocTagInfo.isValidName(tagName)) {
                    tags.add(new JavadocTag(line, col, tagName, tagValue));
                } else {
                    invalidTags.add(new InvalidJavadocTag(line, col, tagName));
                }
                // else Error: Unexpected match count for inline Javadoc
                // tag!
            }
        }
        blockTagPattern = Pattern.compile("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
    }
    return new JavadocTags(tags, invalidTags);
}

From source file:com.puppycrawl.tools.checkstyle.utils.JavadocUtils.java

/**
 * Gets validTags from a given piece of Javadoc.
 * @param textBlock// w w w . j a  v  a  2 s.c o m
 *        the Javadoc comment to process.
 * @param tagType
 *        the type of validTags we're interested in
 * @return all standalone validTags from the given javadoc.
 */
public static JavadocTags getJavadocTags(TextBlock textBlock, JavadocTagType tagType) {
    final String[] text = textBlock.getText();
    final List<JavadocTag> tags = Lists.newArrayList();
    final List<InvalidJavadocTag> invalidTags = Lists.newArrayList();
    Pattern blockTagPattern = Pattern.compile("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
    for (int i = 0; i < text.length; i++) {
        final String textValue = text[i];
        final Matcher blockTagMatcher = blockTagPattern.matcher(textValue);
        if ((tagType == JavadocTagType.ALL || tagType == JavadocTagType.BLOCK) && blockTagMatcher.find()) {
            final String tagName = blockTagMatcher.group(1);
            String content = textValue.substring(blockTagMatcher.end(1));
            if (content.endsWith("*/")) {
                content = content.substring(0, content.length() - 2);
            }
            final int line = textBlock.getStartLineNo() + i;
            int col = blockTagMatcher.start(1) - 1;
            if (i == 0) {
                col += textBlock.getStartColNo();
            }
            if (JavadocTagInfo.isValidName(tagName)) {
                tags.add(new JavadocTag(line, col, tagName, content.trim()));
            } else {
                invalidTags.add(new InvalidJavadocTag(line, col, tagName));
            }
        }
        // No block tag, so look for inline validTags
        else if (tagType == JavadocTagType.ALL || tagType == JavadocTagType.INLINE) {
            lookForInlineTags(textBlock, i, tags, invalidTags);
        }
        blockTagPattern = Pattern.compile("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
    }
    return new JavadocTags(tags, invalidTags);
}

From source file:au.com.borner.salesforce.client.rest.domain.CompilerError.java

public Pair<Integer, Integer> getLocation() {
    String problem = getProblem();
    Matcher matcher = pattern.matcher(problem);
    if (matcher.matches()) {
        int start = matcher.start(1);
        int end = matcher.end(1);
        Integer row = Integer.parseInt(problem.substring(start, end));
        start = matcher.start(2);/* w  w w.  j  a  v  a  2 s . c  o m*/
        end = matcher.end(2);
        Integer column = Integer.parseInt(problem.substring(start, end));
        adjustedProblem = problem.substring(end);
        return new Pair<Integer, Integer>(row, ++column);
    } else {
        return null;
    }
}