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 void main(String[] args) {
    Pattern pattern = null;//w  ww  .  j  a  v  a 2s.  com
    Matcher matcher = null;

    Console console = System.console();
    while (true) {
        try {
            pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            matcher = pattern.matcher(console.readLine("Enter input string to search: "));
        } catch (PatternSyntaxException pse) {
            console.format("There is a problem with the regular expression!%n");
            console.format("The pattern in question is: %s%n", pse.getPattern());
            console.format("The description is: %s%n", pse.getDescription());
            console.format("The message is: %s%n", pse.getMessage());
            console.format("The index is: %s%n", pse.getIndex());
            System.exit(0);
        }
        boolean found = false;
        while (matcher.find()) {
            console.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
            found = true;
        }
        if (!found) {
            console.format("No match found.%n");
        }
    }
}

From source file:Main.java

public static int indexOf(Pattern pattern, String s) {
    Matcher matcher = pattern.matcher(s);
    return matcher.find() ? matcher.start() : -1;
}

From source file:Main.java

/**
 * Returns local name of the root element of the XML document represented
 * by the given string, or <code>null</code>, when the given string does
 * not contain valid XML.//  w  w w . j a  va 2 s  . c  om
 * @param s
 *       XML string.
 * @return
 *       root element local name, or <code>null</code> when it could not be determined.
 */
public static String rootElementName(String s) {
    if (s == null) {
        return null;
    }
    Matcher matcher = ROOT_ELEMENT_PATTERN.matcher(s);
    return (matcher.find() && (matcher.start() == 0)) ? matcher.group(1) : null;
}

From source file:Main.java

/**
 * //from w  w  w  .  ja  va  2  s. com
 * @param tagName
 * @param content
 * @param fromIndex
 * @return
 */
public static int findTagBegin(String tagName, CharSequence content, int fromIndex) {
    Pattern PTN_TAG_BEGIN = Pattern.compile("<" + tagName + "[\\s>]");
    Matcher matcher = PTN_TAG_BEGIN.matcher(content);
    if (matcher.find(fromIndex)) {
        return matcher.start();
    } else {
        return -1;
    }
}

From source file:Main.java

public static CharSequence scaleEmotions(String text) {
    SpannableString spannableString = new SpannableString(text);
    for (String emotion : emotions) {
        Pattern pattern = Pattern.compile(emotion);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(new RelativeSizeSpan(1.2f), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }/*from w  w  w .  ja  va 2 s.co  m*/
    }
    return spannableString;
}

From source file:Main.java

public static CharSequence htmlfrom(CharSequence text) {
    Pattern htmlflag1 = Pattern.compile("<(.*?)>");
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    Matcher matcher = htmlflag1.matcher(text);
    while (matcher.find()) {
        builder.delete(matcher.start(), matcher.end());
        text = builder;/*from   ww w .  jav a 2s  .  co  m*/
        matcher = htmlflag1.matcher(text);
    }
    Pattern htmlflag2 = Pattern.compile("&(.*?);");
    matcher = htmlflag2.matcher(text);
    while (matcher.find()) {
        builder.delete(matcher.start(), matcher.end());
        text = builder;
        matcher = htmlflag2.matcher(text);
    }

    return builder.toString();

}

From source file:Main.java

public static String getAttributeNameFromColName(String tableName) {
    Pattern p = Pattern.compile("_+(\\w?)");
    Matcher m = p.matcher(tableName);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        if (m.start() != 0)
            m.appendReplacement(sb, m.group(1).toUpperCase());
        else/*from   w w w  .  ja  v a 2  s  . c  om*/
            m.appendReplacement(sb, m.group(1).toLowerCase());
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static List<String> extractUrl(String text) {
    List<String> urls = new ArrayList<>();
    Matcher matcher = Patterns.WEB_URL.matcher(text);
    while (matcher.find()) {
        int start = matcher.start();
        if (start > 0 && text.charAt(start - 1) == '@') {
            continue;
        }/*from  w  ww .  j a  v  a2s  . co m*/
        String url = matcher.group();
        if (!url.startsWith("http")) {
            url = "http://" + url;
        }
        urls.add(url);
    }
    return urls;
}

From source file:Main.java

public static List<int[]> getLinksRanges(String s) {
    List<int[]> ranges = new ArrayList<int[]>();
    Matcher matcher = WEB_URL_PATTERN.matcher(s);

    while (matcher.find()) {
        int start = matcher.start();
        int end = matcher.end();
        ranges.add(new int[] { start, end });
    }//  ww w.  j  av a 2  s.c  o  m
    return ranges;
}

From source file:Main.java

public static String makeCanonical(String classname) {
    Matcher matcher = PLAIN_NAME_START_PATTERN.matcher(classname);
    if (matcher.find()) {
        return classname.substring(0, matcher.start() + 1)
                + classname.substring(matcher.start() + 1, classname.length()).replace('.', '$');
    } else {/*  w  ww.  j a v  a  2s  .co m*/
        throw new IllegalArgumentException("Unable to identify the canonical name of " + classname);
    }
}