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.nextep.designer.sqlgen.oracle.parser.OraclePackageParser.java

@Override
public String parseName(String sql) {
    String tag = null;//from   w ww .  j  a v a 2s  .  co  m
    if (isBody(sql)) {
        tag = "body"; //$NON-NLS-1$
    } else {
        tag = "package"; //$NON-NLS-1$
    }
    // Extracting procedure or function name from the SQL source
    Pattern pattern = Pattern.compile("\\s*(" + tag + ")\\s+((\\w)+)"); //$NON-NLS-1$ //$NON-NLS-2$ 
    Matcher m = pattern.matcher(sql.toLowerCase());
    // Looking for first occurrence
    String parsedName = null;
    if (m.find()) {
        parsedName = sql.substring(m.start(2), m.end(2));
    }
    return parsedName;
}

From source file:com.phoenixnap.oss.ramlapisync.javadoc.JavaDocEntry.java

/**
 * Removes {@link or other {@ notation from the javadoc and retains the enclosed data
 * //from   w ww .  j av  a 2 s.c  o  m
 * @param target The target comment to clean
 * @return The comment cleaned from the target characters
 */
private String cleanLinks(String target) {
    Matcher linkMatcher = LINK_BLOCK.matcher(target);
    // Build the main comment first
    while (linkMatcher.find()) {
        try {
            target = target.substring(0, linkMatcher.start(0)) + linkMatcher.group(1)
                    + target.substring(linkMatcher.end(1) + 1);
        } catch (Exception ex) {
            //do nothing
        }
    }
    return target;
}

From source file:net.healeys.lexic.online.OnlineGame.java

public Iterator<Score> getScores() {
    Pattern startPat = Pattern.compile("^!START:([^,]+),(\\d+),(-?\\d+)$");
    Pattern endPat = Pattern.compile("^!END$");

    for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
        try {/*ww  w .  j a  va2s .c o  m*/
            LinkedList<Score> scores = new LinkedList<Score>();

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(BASE_URL + urls.get("score"));
            addHeaders(get);

            HttpResponse resp = httpClient.execute(get);

            BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));

            String username = "";
            int points = 0;
            int score = 0;
            String line;
            StringBuffer sb = new StringBuffer(0);
            while ((line = br.readLine()) != null) {
                // Log.d(TAG,"line:"+line);
                Matcher mat;
                if ((mat = startPat.matcher(line)).find()) {
                    // Log.d(TAG,"startPat matched"+line);
                    username = line.substring(mat.start(1), mat.end(1));
                    points = Integer.parseInt(line.substring(mat.start(2), mat.end(2)));
                    score = Integer.parseInt(line.substring(mat.start(3), mat.end(3)));
                    sb = new StringBuffer(2048);
                } else if ((mat = endPat.matcher(line)).find()) {
                    // Log.d(TAG,"endPat matched"+line);
                    scores.add(new Score(username, score, points, sb.toString()));
                } else if (sb != null && line.length() > 1) {
                    sb.append(line);
                    sb.append('\n');
                }
            }

            return scores.iterator();
        } catch (Exception e) {
            // Log.e(TAG,"getScores error",e);
        }
    }

    return null;
}

From source file:de.tudarmstadt.ukp.dkpro.core.textnormalizer.transformation.HyphenationRemover.java

@Override
public void process(JCas aInput, JCas aOutput) throws AnalysisEngineProcessException {
    StringBuilder c_new = new StringBuilder();
    final Matcher m = HYPHEN_PATTERN.matcher(aInput.getDocumentText());
    while (m.find()) {
        // The capturing groups count should be exactly 2.
        assert m.groupCount() == 2 : "Expected 2 groups but got " + m.groupCount();

        c_new.setLength(0);
        c_new.append(m.group(1));
        c_new.append(m.group(2));

        if (dict.contains(c_new.toString())) {
            replace(m.start(1), m.end(2), c_new.toString());
            //                getLogger().info(
            //                        "Conflated: [" + aInput.getDocumentText().substring(m.start(1), m.end(2))
            //                                + "] to [" + c_new + "]");
        }/*from   w w  w.j av a  2 s. com*/
    }
}

From source file:mobi.jenkinsci.server.core.net.ProxyUtil.java

private String getSubstringBetweenLinks(int linkIndex, String content, Matcher m, int lastEnd) {
    return content.substring(lastEnd, m.start(linkIndex));
}

From source file:edu.uab.ccts.nlp.uima.annotator.SegmentRegexAnnotator.java

/**
 * Add Segment annotations to the cas. First create a list of segments. Then
 * sort the list according to segment start. For each segment that has no
 * end, set the end to the [beginning of next segment - 1], or the eof.
 *///from   w  w w . ja v a2  s.c  o m
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    log.info("Starting SegmentRegexAnnotator with " + regexMap.size() + " segements.");
    String strDocText = aJCas.getDocumentText();
    if (strDocText == null)
        return;
    List<Segment> segmentsAdded = new ArrayList<Segment>();
    // find all the segments, set begin and id, add to list
    for (Map.Entry<SegmentRegex, Pattern> entry : regexMap.entrySet()) {
        if (log.isDebugEnabled()) {
            log.debug("applying regex:" + entry.getKey().getRegex());
        }
        Matcher matcher = entry.getValue().matcher(strDocText);
        while (matcher.find()) {
            Segment seg = new Segment(aJCas);
            if (entry.getKey().isLimitToRegex() && matcher.groupCount() == 1) {
                seg.setBegin(matcher.start(1));
                seg.setEnd(matcher.end(1));
            } else {
                seg.setBegin(matcher.start());
                if (entry.getKey().isLimitToRegex()) {
                    seg.setEnd(matcher.end());
                }
            }
            seg.setId(entry.getKey().getSegmentID());
            //if (log.isDebugEnabled()) {
            log.debug("found match: id=" + seg.getId() + ", begin=" + seg.getBegin() + " end=" + seg.getEnd());
            //}
            segmentsAdded.add(seg);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("segmentsAdded: " + segmentsAdded.size());
    }
    if (segmentsAdded.size() > 0) {
        // sort the segments by begin
        Collections.sort(segmentsAdded, new Comparator<Segment>() {

            // @Override
            public int compare(Segment o1, Segment o2) {
                return o1.getBegin() < o2.getBegin() ? -1 : o1.getBegin() > o2.getBegin() ? 1 : 0;
            }

        });
        // set the end for each segment
        for (int i = 0; i < segmentsAdded.size(); i++) {
            Segment seg = segmentsAdded.get(i);
            Segment segNext = (i + 1) < segmentsAdded.size() ? segmentsAdded.get(i + 1) : null;
            if (seg.getEnd() <= 0) {
                if (segNext != null) {
                    // set end to beginning of next segment
                    seg.setEnd(segNext.getBegin() - 1);
                } else {
                    // set end to doc end
                    seg.setEnd(strDocText.length());
                }
            } else {
                // segments shouldn't overlap
                if (segNext != null && segNext.getBegin() < seg.getEnd()) {
                    seg.setEnd(segNext.getBegin() - 1);
                }
            }
            //if (log.isDebugEnabled()) {
            log.debug("Adding Segment: segment id=" + seg.getId() + ", begin=" + seg.getBegin() + ", end="
                    + seg.getEnd());
            //}
            seg.addToIndexes();
        }
    }
    // ctakes 1.3.2 - anything not in a segment will not be annotated - add
    // text outside segments to the 'default' segment
    int end = 0;
    for (Segment seg : segmentsAdded) {
        if ((seg.getBegin() - 1) > end) {
            addGapSegment(aJCas, end, seg.getBegin() - 1);
        }
        end = seg.getEnd();
    }
    if (end < strDocText.length()) {
        addGapSegment(aJCas, end, strDocText.length());
    }
}

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

@Override
public void replaceEntityName(String newName) {
    Matcher entityMatcher = FROM_ENTITY_PATTERN.matcher(buffer);
    if (entityMatcher.find()) {
        buffer.replace(entityMatcher.start(FEP_ENTITY), entityMatcher.end(FEP_ENTITY), newName);
        return;/*from   w  w  w  .  jav a2 s . c om*/
    }
    error("Unable to find entity name");
}

From source file:GIST.IzbirkomExtractor.AbbrList.java

/**
 * Creates a list of all possible expansions of a given streetName.
 * stretName itself will be the first element of the expansion list.
 * /*ww  w  .  j  av a 2  s.  co m*/
 * @param streetName
 * @return
 */
public ArrayList<String> createAllExpansions(String streetName) {

    ArrayList<String> expansions = new ArrayList<String>();
    expansions.add(streetName);

    Matcher mat = getAbbreviationsPattern().matcher(streetName);
    if (mat.find()) {
        for (String exp : abbrevs.get(mat.group(1)).getExpansions()) {
            StringBuffer str = new StringBuffer(mat.replaceFirst(exp));

            /* get rid of abbreviations with dash */
            if (streetName.length() > mat.start(1) + 1 && streetName.charAt(mat.start(1) + 1) == '-') {
                str.setCharAt(mat.start(1) + exp.length(), ' ');
            }

            if (exp.endsWith("-")) { // support for ?-, ?-, -; remove space, downcase following letter

                /* skip replacement and the end of string */
                if (str.toString().endsWith(exp))
                    continue;

                str.delete(mat.start(1) + exp.length() - 1, mat.start(1) + exp.length() + 1);
                char upcase = str.charAt(mat.start(1) + exp.length() - 1);
                str.setCharAt(mat.start(1) + exp.length() - 1, Character.toLowerCase(upcase));
            }

            expansions.add(str.toString());
        }
    }

    return expansions;
}

From source file:com.nextep.designer.sqlgen.oracle.parser.OraclePackageParser.java

/**
 * Renames the provided SQL string by the new name considering that the name will follow the
 * supplied token//  w  ww  . jav  a  2  s .  com
 * 
 * @param sql entire SQL source to rename
 * @param newName new name to inject in the SQL source
 * @param firstTokenBeforeName first token before the name information
 * @return the full SQL, renamed
 */
private String renameSqlHeader(String sql, String newName, String firstTokenBeforeName) {
    // Extracting procedure or function name from the SQL source
    Pattern pattern = Pattern.compile("\\s*(" + firstTokenBeforeName + ")\\s+((\\w)+)"); //$NON-NLS-1$ //$NON-NLS-2$
    String lowerCased = sql.toLowerCase();
    Matcher m = pattern.matcher(lowerCased);
    String newSql = sql;
    // Looking for first occurrence
    if (m.find()) {
        // Building the "renamed" SQL source declaration
        newSql = sql.substring(0, m.start(2)) + newName + sql.substring(m.end(2));
    }
    return newSql;
}

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

@Override
public void handleCaseInsensitiveParam(String paramName) {
    Pattern pattern = Pattern.compile(COND_PATTERN_REGEX + ":" + paramName, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(buffer);
    if (matcher.find()) {
        String field = matcher.group(1);
        buffer.replace(matcher.start(1), matcher.end(1), "lower(" + field + ")");
    }/*from   w w  w .j a v  a 2  s  .co m*/
}