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:Main.java

public static String searchAndReturn(String source, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(source);
    boolean b = matcher.find();
    if (b) {//from  ww w  . j av  a2s.  co m
        return source.substring(matcher.start(), matcher.end());
    }
    return null;
}

From source file:disko.ParagraphDetector.java

public static void detectParagraphs(String s, Collection<Ann> annotations) {
    Matcher m = paragraphSplitter.matcher(s);
    int start = 0;
    while (m.find()) {
        int end = m.start();
        String found = s.subSequence(start, end).toString();
        ParagraphAnn ann = new ParagraphAnn(start, end, found);
        annotations.add(ann);//from   w ww.j  a v  a 2 s  .co  m
        log.debug("Found ParagraphAnn " + ann);
        start = m.end();
    }
    if (annotations.isEmpty() && s.trim().length() > 0)
        annotations.add(new ParagraphAnn(0, s.length(), s));
}

From source file:Main.java

public static String getPlainName(String qualifiedName) {
    Matcher matcher = PLAIN_NAME_START_PATTERN.matcher(qualifiedName);
    if (matcher.find()) {
        return qualifiedName.substring(matcher.start() + 1, qualifiedName.length());
    } else {/*w  w w  .j av a  2 s  .  c o m*/
        return qualifiedName;
    }
}

From source file:Main.java

/**
 * Takes the characters until the pattern is matched
 */// w  w  w  .  j  a  v  a 2  s.c  o  m
public static String takeUntil(Object src, Object pattern) {
    String src2 = src.toString();
    Matcher matcher = Pattern.compile(pattern.toString()).matcher(src2);

    if (!matcher.find())
        return src2;

    int index = matcher.start();

    if (index == -1) {
        return src2;
    }
    return src2.substring(0, index);
}

From source file:Main.java

private static void match(String REGEX, String INPUT) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT);
    int count = 0;

    while (m.find()) {
        count++;/*from  w  w  w  .  ja  v a2s .c  o  m*/
        System.out.println("\nMatch number: " + count);
        System.out.println("start() : " + m.start());
        System.out.println("end()   : " + m.end());
        System.out.println("group() : " + m.group());
    }
}

From source file:Main.java

private static int[] getIndex(String text, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(text);
    int[] data = new int[2];
    // Check all occurrences
    while (matcher.find()) {
        data[0] = matcher.start();
        data[1] = matcher.end();/*from   w ww  .  j  a  va 2 s  .  com*/
        //            System.out.print("Start index: " + matcher.start());
        //            System.out.print(" End index: " + matcher.end());
        //            System.out.println(" Found: " + matcher.group());
    }
    return data;
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {//from  www.ja  v  a 2  s. co m
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
    }

    return ss;
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/*  w w w .j  ava2  s . com*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        // Logger.e(TAG, ex);
    }

    return ss;
}

From source file:Main.java

public static void findPattern(String regex, String source) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(source);

    System.out.println("Regex:" + regex);
    System.out.println("Text:" + source);
    while (m.find()) {
        System.out.println("Matched  Text:" + m.group() + ", Start:" + m.start() + ", " + "End:" + m.end());
    }/*  ww w  .  ja  va 2 s. c  om*/
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {/*from w w w .j  a v a2s  . com*/
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ss;
}