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

/**
 * Simple matcher that finds given regular expression patterns.
 * Samples:<br />/* ww  w .java 2s .  co  m*/
 * contains("selam", new String[]{"q", "r"}) -> returns false.<br />
 * contains("selam", new String[]{"q", "a"}) -> returns true.<br />
 * contains("selam", new String[]{"q", "a$"}) -> returns false.<br />
 * contains("selam", new String[]{"q", "m$"}) -> returns true.<br />
 * contains("selam", new String[]{"^m"}) -> returns false.<br />
 *
 * @param str Source string to search for.
 * @param regularExpressions Regular Expressions as an array.
 * @return TRUE if ANY of the given patterns match. FALSE, if ALL of the given patterns are not found.
 */
public static boolean contains(String str, String[] regularExpressions) {
    for (String regularExpression : regularExpressions) {
        Pattern pattern = Pattern.compile(regularExpression);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return true;
        }
    }
    return false;
}

From source file:com.titankingdoms.dev.titanchat.format.Censor.java

/**
 * Filters the text for phrases and censors the phrases with the censor
 * //  ww w  .  j a  v  a 2s .  c  om
 * @param text The text to filter
 * 
 * @param phrases The phrases to censor
 * 
 * @param censor The censor to use
 * 
 * @return The filtered text
 */
public static String filter(String text, List<String> phrases, String censor) {
    if (text == null)
        return "";

    if (phrases == null)
        return text;

    if (censor == null)
        censor = "";

    StringBuffer filtered = new StringBuffer();

    String regex = "(" + StringUtils.join(phrases.toArray(new String[0])) + ")";

    Pattern phrasePattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher match = phrasePattern.matcher(text);

    while (match.find())
        match.appendReplacement(filtered, Pattern.compile(".").matcher(match.group()).replaceAll(censor));

    return match.appendTail(filtered).toString();
}

From source file:Main.java

public static String toHref(String title) {
    StringBuffer sb = new StringBuffer(title);
    Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher mat = pat.matcher(title);
    int index = 0;
    int index1 = 0;
    while (mat.find()) {
        String url = mat.group();
        //System.out.println(url);
        if (url.indexOf("http://") != 0)
            url = "http://" + url;
        Object obj[] = { "'" + url + "'" };
        String a = MessageFormat.format(A1, obj);
        int l = a.length();
        index += index1;//from  ww  w  . j a  v a 2s  .co m
        sb.insert(mat.start() + index, a);
        index += l;
        sb.insert((mat.end()) + index, A2);
        index1 = A2.length();
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Remove xml comments from the xml string.
 * //  w  ww .  j a  va2s.  com
 * @param xml
 * @return
 */
public static String stripXMLComment(CharSequence xml) {
    if (xml == null) {
        return null;
    }

    // option DOTALL: '.' matches newlines
    // use '.+?' in stead of '.*': non-greedy matching ("<!-- --> <important-stuff\> <!-- -->" is replaced with " <important-stuff\> ")
    Pattern p = Pattern.compile("<!--.+?-->", Pattern.DOTALL); //$NON-NLS-1$
    Matcher m = p.matcher(xml);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, ""); //$NON-NLS-1$
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String oldEncodeQrCodeString(String text) {
    Pattern pattern = Pattern.compile("[A-Z]");
    Matcher matcher = pattern.matcher(text);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String letter = matcher.group(0);
        matcher.appendReplacement(sb, QR_CODE_LETTER + letter);
    }/*from   w  ww.  j  a  v a2 s  .  co m*/
    matcher.appendTail(sb);

    return sb.toString().toUpperCase(Locale.US);
}

From source file:com.qumoon.commons.TaobaoUtils.java

/**
 * ?? url ? num iid//from www.j  av a2s .  com
 */
public static Long getItemUrlNumIid(String url) {
    Matcher matcher = ITEM_NUM_IID_PATTERN.matcher(url);
    if (matcher.find() && matcher.groupCount() > 1) {
        String numIidStr = matcher.group(2);
        if (NumberUtils.isNumber(numIidStr)) {
            return Long.valueOf(numIidStr);
        } else {
            return null;
        }
    }
    return null;
}

From source file:Utils.java

public static List<String> getFound(String contents, String regex) {
    if (isEmpty(regex) || isEmpty(contents)) {
        return null;
    }// ww w.ja  v a2 s.  c om
    List<String> results = new ArrayList<String>();
    Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
    Matcher matcher = pattern.matcher(contents);

    while (matcher.find()) {
        if (matcher.groupCount() > 0) {
            results.add(matcher.group(1));
        } else {
            results.add(matcher.group());
        }
    }
    return results;
}

From source file:brooklyn.entity.cloudfoundry.LocalResourcesDownloader.java

public static String findArchiveNameFromUrl(String url) {
    String name = url.substring(url.lastIndexOf('/') + 1);
    if (name.indexOf("?") > 0) {
        Pattern p = Pattern.compile("[A-Za-z0-9_\\-]+\\..(ar|AR)($|(?=[^A-Za-z0-9_\\-]))");
        Matcher wars = p.matcher(name);
        if (wars.find()) {
            name = wars.group();//from  w w w.j av  a  2 s  .  co  m
        }
    }
    return name;
}

From source file:Main.java

/**
 * toSpannableString//from w  ww.jav a 2  s  .c  om
 * @return SpannableString
 * @throws
 */
public static SpannableString toSpannableString(Context context, String text) {
    if (!TextUtils.isEmpty(text)) {
        SpannableString spannableString = new SpannableString(text);
        int start = 0;
        Pattern pattern = Pattern.compile("\\\\ue[a-z0-9]{3}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            String faceText = matcher.group();
            String key = faceText.substring(1);
            BitmapFactory.Options options = new BitmapFactory.Options();
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                    context.getResources().getIdentifier(key, "drawable", context.getPackageName()), options);
            ImageSpan imageSpan = new ImageSpan(context, bitmap);
            int startIndex = text.indexOf(faceText, start);
            int endIndex = startIndex + faceText.length();
            if (startIndex >= 0)
                spannableString.setSpan(imageSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            start = (endIndex - 1);
        }

        return spannableString;
    } else {
        return new SpannableString("");
    }
}

From source file:de.snertlab.xdccBee.irc.DccMessageParser.java

public static boolean isDccMessage(String message) {
    String s = cleanMessage(message);
    Matcher m = DCC_MESSAGE_PATTER.matcher(s);
    if (m.find()) {
        return true;
    }//from ww  w. j  av  a 2 s.  c o  m
    return false;
}