Example usage for java.util.regex Matcher find

List of usage examples for java.util.regex Matcher find

Introduction

In this page you can find the example usage for java.util.regex Matcher find.

Prototype

public boolean find() 

Source Link

Document

Attempts to find the next subsequence of the input sequence that matches the pattern.

Usage

From source file:Main.java

public static String getModel(String useragent) {
    Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
    Matcher matcher = pattern.matcher(useragent);
    String model = null;/*from   w  w w. j av a  2  s . co  m*/
    if (matcher.find()) {
        model = matcher.group(1).trim();
    }
    return model;
}

From source file:Main.java

public static boolean isAddress(String address) {
    int i = 0, j = 0, k = 0, u = 0;
    int count = address.length();
    Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
    Matcher m = pattern.matcher(address);
    while (m.find()) {
        i++;//w  ww . j  av  a 2 s . c  o m
    }
    for (int idx = 0; idx < count; idx++) {
        char c = address.charAt(idx);
        int tmp = (int) c;
        if ((tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z')) {
            j++;
        }
        if (Character.isDigit(address.charAt(idx))) {
            k++;
        }
        if (c == ' ') {
            u++;
        }
    }
    if ((i + j + k + u) == count) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

/**
 * Validates and processes the given Url
 * @param    url The given Url to process
 * @return   Pre-process Url as string */
public static String cleanUrl(StringBuilder url) {
    //ensure that the urls are absolute
    Pattern pattern = Pattern.compile("^(https?://[^/]+)");
    Matcher matcher = pattern.matcher(url);
    if (!matcher.find())
        throw new IllegalArgumentException("Invalid Url format.");

    //get the http protocol match
    String protocol = matcher.group(1);

    //remove redundant forward slashes
    String query = url.substring(protocol.length());
    query = query.replaceAll("//+", "/");

    //return process url
    return protocol.concat(query);
}

From source file:Main.java

public static String getYoutubeVideoId(String in) {

    //*EDIT* - fixed to hopefully support more recent youtube link styles/formats:
    final String pattern = "(?<=watch\\?v=|/videos/|/embed/|youtu.be/)[^&#?]*";
    final String pattern2 = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";

    Pattern compiledPattern = Pattern.compile(pattern2, Pattern.CASE_INSENSITIVE);
    Matcher matcher = compiledPattern.matcher(in);

    if (matcher.find()) {
        return matcher.group(1);
    }//from  w  w w . j a v a 2  s. c  o  m

    return null;
}

From source file:Main.java

public static <T extends Collection<String>> T findAll(String regex, String content, int group, T collection) {
    Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
    while (matcher.find()) {
        collection.add(matcher.group(group));
    }//from   w w w . j a v a2  s.  c  o m
    return collection;
}

From source file:Main.java

public static String get(String regex, String content, int groupIndex) {
    Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
    if (matcher.find()) {
        return matcher.group(groupIndex);
    }/*w  w w .j  a v a 2  s  . c  om*/
    return null;
}

From source file:com.streamsets.pipeline.lib.jdbc.parser.sql.RawTypeHandler.java

public static byte[] parseRaw(String column, String value, int columnType) throws StageException {
    if (value == null) {
        return null;
    }/*from   w  ww . j  a v a 2 s. c  om*/
    Matcher m = HEX_TO_RAW_PATTERN.matcher(value);
    if (m.find()) {
        try {
            return Hex.decodeHex(m.group(1).toCharArray());
        } catch (DecoderException e) {
            throw new StageException(JDBC_204, m.group(1));
        }
    }
    throw new UnsupportedFieldTypeException(column, value, columnType);
}

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.//  ww w .  j  a  v a  2  s.c o  m
 * @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

public static String getNumber(String input) {
    if (input == null)
        return "";
    String str = input.replaceAll("\\s+", "");
    Pattern number = Pattern.compile("[0-9]+");
    Matcher matcher1 = number.matcher(str);
    if (matcher1.find())
        return matcher1.group(0);
    return "";
}

From source file:com.gooddata.http.client.TokenUtils.java

static String extractToken(final HttpResponse response) throws IOException {
    final String responseBody = response == null || response.getEntity() == null ? ""
            : EntityUtils.toString(response.getEntity());
    final Matcher matcher = pattern.matcher(responseBody);
    if (!matcher.find()) {
        throw new GoodDataAuthException("Unable to login. Malformed response body: " + responseBody);
    }//from   w  w  w .java 2s  .  c  om
    final String token = matcher.group(1);
    if (token == null) {
        throw new GoodDataAuthException("Unable to login. Malformed response body: " + responseBody);
    }
    return token;
}