Example usage for java.util.regex Pattern CASE_INSENSITIVE

List of usage examples for java.util.regex Pattern CASE_INSENSITIVE

Introduction

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

Prototype

int CASE_INSENSITIVE

To view the source code for java.util.regex Pattern CASE_INSENSITIVE.

Click Source Link

Document

Enables case-insensitive matching.

Usage

From source file:eu.nerdz.api.impl.fastreverse.messages.FastReverseConversationHandler.java

/**
 * Replaces a composite tag./*  ww w  .j  a v  a 2 s .  c  o m*/
 * @param regex A regex
 * @param message A message to be parsed
 * @param format A format for pretty formatting. Only 2 string fields.
 * @return A string in which all occurrences of regex have been substituted with the contents matched
 */
private static String replaceDouble(String regex, String message, String format) {

    Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message);
    StringBuffer result = new StringBuffer();

    while (matcher.find()) {
        matcher.appendReplacement(result,
                String.format(format, matcher.group(1), matcher.group(2)).replace("$", "\\$"));
    }

    matcher.appendTail(result);

    return result.toString();

}

From source file:com.jabyftw.easiercommands.Util.java

/**
 * Source: Essentials (found through Ban-Management)
 * <b>Letters used:</b> y mo w d h m s
 *
 * @param time string with the time, eg: "3w4h" - three weeks and four hours
 * @return the time in milliseconds//from  w  w  w  .j  a v  a  2s.  c  om
 * @see <a href=https://github.com/BanManagement/BanManager/blob/master/src/main/java/me/confuser/banmanager/util/DateUtils.java>Credits to Essentials</a>
 */
public static long parseTimeDifference(@NotNull String time) {
    Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
    Matcher matcher = timePattern.matcher(time);

    int years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0;
    boolean found = false;

    while (matcher.find()) {
        if (matcher.group() == null || matcher.group().isEmpty()) {
            continue;
        }

        for (int i = 0; i < matcher.groupCount(); i++) {
            if (matcher.group(i) != null && !matcher.group(i).isEmpty()) {
                found = true;
                break;
            }
        }

        if (found) {
            if (matcher.group(1) != null && !matcher.group(1).isEmpty())
                years = Integer.parseInt(matcher.group(1));
            if (matcher.group(2) != null && !matcher.group(2).isEmpty())
                months = Integer.parseInt(matcher.group(2));
            if (matcher.group(3) != null && !matcher.group(3).isEmpty())
                weeks = Integer.parseInt(matcher.group(3));
            if (matcher.group(4) != null && !matcher.group(4).isEmpty())
                days = Integer.parseInt(matcher.group(4));
            if (matcher.group(5) != null && !matcher.group(5).isEmpty())
                hours = Integer.parseInt(matcher.group(5));
            if (matcher.group(6) != null && !matcher.group(6).isEmpty())
                minutes = Integer.parseInt(matcher.group(6));
            if (matcher.group(7) != null && !matcher.group(7).isEmpty())
                seconds = Integer.parseInt(matcher.group(7));
            break;
        }
    }

    if (!found)
        throw new IllegalArgumentException("Date can't be parsed");
    if (years > 20)
        throw new IllegalArgumentException("Date is too big");

    Calendar calendar = new GregorianCalendar();

    if (years > 0)
        calendar.add(Calendar.YEAR, years);
    if (months > 0)
        calendar.add(Calendar.MONTH, months);
    if (weeks > 0)
        calendar.add(Calendar.WEEK_OF_YEAR, weeks);
    if (days > 0)
        calendar.add(Calendar.DAY_OF_MONTH, days);
    if (hours > 0)
        calendar.add(Calendar.HOUR_OF_DAY, hours);
    if (minutes > 0)
        calendar.add(Calendar.MINUTE, minutes);
    if (seconds > 0)
        calendar.add(Calendar.SECOND, seconds);

    return calendar.getTimeInMillis() - System.currentTimeMillis();
}

From source file:com.norconex.commons.lang.url.URLNormalizer.java

/**
 * Converts the scheme and host to lower case.<p>
 * <code>HTTP://www.Example.com/ &rarr; http://www.example.com/</code>
 * @return this instance//from  w  w  w . jav  a 2s.c  o m
 */
public URLNormalizer lowerCaseSchemeHost() {
    URI u = toURI(url);
    url = Pattern.compile(u.getScheme(), Pattern.CASE_INSENSITIVE).matcher(url)
            .replaceFirst(u.getScheme().toLowerCase());
    url = Pattern.compile(u.getHost(), Pattern.CASE_INSENSITIVE).matcher(url)
            .replaceFirst(u.getHost().toLowerCase());
    return this;
}

From source file:org.opencron.common.utils.StringUtils.java

/**
 * html// w  w w .j  a  v  a 2s . c o m
 *
 * @param htmlStr
 * @return writer:<a href="mailto:benjobs@qq.com">benjobs</a> 2012.2.1
 */
public static String replaceHtml(String htmlStr) {
    if (htmlStr == null || "".equals(htmlStr)) {
        return "";
    }
    String regEx_script = "<script[^>]*?>[\\s\\S]*?</script>"; // script?
    String regEx_style = "<style[^>]*?>[\\s\\S]*?</style>"; // style?
    String regEx_html = "<[^>]+>"; // HTML?

    Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
    Matcher m_script = p_script.matcher(htmlStr);
    htmlStr = m_script.replaceAll(""); // script

    Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
    Matcher m_style = p_style.matcher(htmlStr);
    htmlStr = m_style.replaceAll(""); // style

    Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
    Matcher m_html = p_html.matcher(htmlStr);
    htmlStr = m_html.replaceAll(""); // html

    return htmlStr.trim(); // 
}

From source file:net.longfalcon.newsj.service.MovieService.java

private String parseMovieName(Release release) {
    int categoryId = 0;
    Category category = release.getCategory();
    if (category != null) {
        categoryId = category.getId();/*from  www. j  av a 2  s  . c  om*/
    }
    if (categoryId != CategoryService.CAT_MOVIE_FOREIGN) {
        Pattern movieNamePattern = Pattern.compile("^(?<name>.*)[\\.\\-_\\( ](?<year>19\\d{2}|20\\d{2})",
                Pattern.CASE_INSENSITIVE);
        Matcher movieNameMatcher = movieNamePattern.matcher(release.getSearchName());
        if (movieNameMatcher.find()) {
            String yearString = "";
            try {
                yearString = movieNameMatcher.group("year");
            } catch (IllegalArgumentException iae) {
                // do nothing
            }
            if (ValidatorUtil.isNull(yearString)) {
                movieNamePattern = Pattern.compile(
                        "^(?<name>.*)[\\.\\-_ ](?:dvdrip|bdrip|brrip|bluray|hdtv|divx|xvid|proper|repack|real\\.proper|sub\\.?fix|sub\\.?pack|ac3d|unrated|1080i|1080p|720p)",
                        Pattern.CASE_INSENSITIVE);
                movieNameMatcher = movieNamePattern.matcher(release.getSearchName());
            }

            String nameString = movieNameMatcher.group("name");
            nameString = nameString.replaceAll("\\(.*?\\)|\\.|_", " ");
            if (ValidatorUtil.isNotNull(yearString)) {
                yearString = String.format(" (%s)", yearString);
            }
            return nameString.trim() + yearString;
        }
    }
    return null;
}

From source file:com.orion.console.UrT42Console.java

/**
 * Retrieve a CVAR from the server//from w w w . jav a2s .  co  m
 * 
 * @author Daniele Pantaleone
 * @param  name The CVAR name
 * @throws RconException If the CVAR could not be retrieved form the server
 * @return The <tt>Cvar</tt> object associated to the given CVAR name or <tt>null</tt> 
 *         if such CVAR is not set on the server
 **/
public Cvar getCvar(String name) throws RconException {

    try {

        String result = this.write(name, true);

        Pattern pattern = Pattern.compile("\\s*\\\"[\\w+]*\\\"\\sis:\\\"(?<value>[\\w:\\.\\-\\\\/]*)\\\".*",
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(result);

        if (matcher.matches()) {

            String value = matcher.group("value");

            if (!value.trim().isEmpty()) {
                this.log.trace("Retrieved CVAR " + name + ": " + value);
                return new Cvar(name, value);
            }

        }

    } catch (RconException e) {
        // Catch and re-throw the same Exception but with more details
        throw new RconException("could not retrieve CVAR " + name, e);
    }

    return null;

}

From source file:com.eyem.bean.GrupoBean.java

public void crearPostGrupo() {
    Post p = new Post();
    Usuario u = usuarioService.buscarPorEmail(usuarioBean.getEmail());
    p.setContenido(contenido);//from  w w  w . j  a  v a  2  s  .  c  o  m
    String pattern = "https?:\\/\\/(?:[0-9A-Z-]+\\.)?(?:youtu\\.be\\/|youtube\\.com\\S*[^\\w\\-\\s])([\\w\\-]{11})(?=[^\\w\\-]|$)(?![?=&+%\\w]*(?:['\"][^<>]*>|<\\/a>))[?=&+%\\w]*";

    Pattern compiledPattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Matcher matcher = compiledPattern.matcher(imagen);
    while (matcher.find()) {
        video = matcher.group();
        imagen = null;
        p.setVideo(video);
    }
    if (null != imagen) {
        try {
            java.net.URL url = new java.net.URL(imagen);
            Logger.getLogger(PostBean.class.getName()).log(Level.SEVERE, null,
                    url.toString() + " subido por " + u.getEmail());
        } catch (MalformedURLException ex) {
            Logger.getLogger(PostBean.class.getName()).log(Level.SEVERE, null,
                    ex + " causado por " + u.getEmail());
            imagen = null;
        }
    }
    p.setImagen(imagen);
    p.setIdPost(System.currentTimeMillis());
    p.setTipo(idGrupo.toString());
    p.setCreador(u);

    postService.crearPost(p);
    contenido = null;
    imagen = null;
    listaPostGrupo = postService.buscarPostGrupo(idGrupo);
}

From source file:de.undercouch.bson4jackson.BsonParserTest.java

@Test
public void parseComplex() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD));
    o.put("Symbol", new Symbol("Test"));
    o.put("ObjectId", new org.bson.types.ObjectId(Integer.MAX_VALUE, -2, Integer.MIN_VALUE));
    Pattern p = Pattern.compile(".*",
            Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
    o.put("Regex", p);

    Map<?, ?> data = parseBsonObject(o);
    assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp"));
    assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol"));
    ObjectId oid = (ObjectId) data.get("ObjectId");
    assertEquals(Integer.MAX_VALUE, oid.getTime());
    assertEquals(-2, oid.getMachine());/*from  ww  w .  j  a  v a  2s .c  om*/
    assertEquals(Integer.MIN_VALUE, oid.getInc());
    Pattern p2 = (Pattern) data.get("Regex");
    assertEquals(p.flags(), p2.flags());
    assertEquals(p.pattern(), p2.pattern());
}

From source file:com.adito.security.DefaultUserDatabase.java

private static <T extends Principal> Collection<T> filterPrincipals(String filter, int maxResults,
        Iterable<T> itr, boolean caseSensitive) {
    Collection<T> principals = new ArrayList<T>();
    String regex = Util.parseSimplePatternToRegExp(filter);
    int patternFlags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
    Pattern pattern = Pattern.compile(regex, patternFlags);

    for (T principal : itr) {
        if (principals.size() < maxResults) {
            boolean matches = pattern.matcher(principal.getPrincipalName()).matches();
            if (matches) {
                principals.add(principal);
            }/* www.ja  v a  2  s  .  c  o m*/
        } else {
            break;
        }
    }
    return principals;
}

From source file:com.hangum.tadpole.rdb.core.editors.main.utils.ExtMakeContentAssistUtil.java

/**
 * /*w  w  w  . ja va 2 s  .c  om*/
 * @param userDB
 * @param strQuery
 * @param strCursor
 */
private String getTableColumnAlias(final UserDBDAO userDB, final String strQuery, final String strCursor) {
    List<String> listName = new ArrayList<>();

    TadpoleMetaData dbMetaData = TadpoleSQLManager.getDbMetadata(userDB);
    String strQuote = dbMetaData.getIdentifierQuoteString();
    String strSeparator = ".";
    String strAlias = "";
    if (StringUtils.indexOf(strCursor, strSeparator) != -1) {
        strAlias = StringUtils.substring(strCursor, 0, StringUtils.indexOf(strCursor, strSeparator));
    }

    if (logger.isDebugEnabled()) {
        logger.debug("query is [" + strQuery + "]");
        logger.debug("[quote]" + strQuote + " [alias]" + strAlias);
    }

    String tableNamePattern = "((?:" + strQuote + "(?:[.[^" + strQuote + "]]+)" + strQuote + ")|(?:[\\w"
            + Pattern.quote(strSeparator) + "]+))";
    String structNamePattern;
    if (StringUtils.isEmpty(strAlias)) {
        structNamePattern = "(?:from|update|join|into)\\s*" + tableNamePattern;
    } else {
        structNamePattern = tableNamePattern + "(?:\\s*\\.\\s*" + tableNamePattern + ")?" + "\\s+(?:(?:AS)\\s)?"
                + strAlias + "[\\s,]+";
    }

    try {
        Pattern aliasPattern = Pattern.compile(structNamePattern, Pattern.CASE_INSENSITIVE);
        Matcher matcher = aliasPattern.matcher(strQuery);
        if (!matcher.find()) {
            if (logger.isDebugEnabled())
                logger.debug("=====> Not found match");
            return "";
        }

        if (matcher.groupCount() > 0) {
            for (int i = 1; i <= matcher.groupCount(); i++) {
                listName.add(matcher.group(i));
            }
        } else {
            if (logger.isDebugEnabled())
                logger.debug("=====> Not found object name");
            return "";
        }

    } catch (PatternSyntaxException e) {
        if (logger.isDebugEnabled())
            logger.error("=====> find pattern exception");
        return "";
    }

    // find object column
    StringBuffer strColumnList = new StringBuffer();
    for (String tblName : listName) {
        strColumnList.append(getAssistColumnList(userDB, tblName)).append(_PRE_GROUP);
    }
    return strColumnList.toString();
}