Example usage for java.util.regex Pattern CANON_EQ

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

Introduction

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

Prototype

int CANON_EQ

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

Click Source Link

Document

Enables canonical equivalence.

Usage

From source file:org.openremote.controller.protocol.telnet.TelnetCommand.java

@Override
public String read(EnumSensorType sensorType, Map<String, String> statusMap) {
    String readResponse = statusDefault;
    String filteredResponse = "";
    send(true);/*  w  w  w .j av a2s .co m*/

    try {
        if ("".equals(getResponseFilter()) || getResponseFilter() == null) {
            filteredResponse = getResponse();
        } else {
            Pattern p = Pattern.compile(getResponseFilter(), Pattern.CANON_EQ | Pattern.UNICODE_CASE);
            Matcher m = p.matcher(getResponse());
            boolean b = m.matches();
            if (b) {
                String matchedGroup = m.group(getResponseFilterGroup());
                if (matchedGroup != null) {
                    filteredResponse = matchedGroup;
                }
            } else {
                logger.error("Telnet Read Status: No Match using Regex: '" + getResponseFilter()
                        + "' on response from command '" + getCommand() + "'");
            }
        }
    } catch (PatternSyntaxException e) {
        System.out.println("Telnet Read Status: REGEX ERROR");
        logger.error("Telnet Read Status: Invalid filter expression", e);
    }

    if (!"".equals(filteredResponse)) {
        switch (sensorType) {
        // Switch: on or off response needed
        case SWITCH:
            filteredResponse.replaceAll("1|on", "true");
            Boolean bool = Boolean.parseBoolean(filteredResponse);
            if (bool) {
                readResponse = "on";
            } else {
                readResponse = "off";
            }
            break;
        case LEVEL:
        case RANGE:
            try {
                Integer intVal = Integer.parseInt(filteredResponse);
                readResponse = filteredResponse;
            } catch (PatternSyntaxException e) {
                logger.info("Can't convert filteredResponse to type Integer: " + e);
            }
            break;
        default:
            readResponse = filteredResponse;
        }
    }

    return readResponse;
}

From source file:org.apache.nifi.processors.standard.EvaluateRegularExpression.java

int getCompileFlags(ProcessContext context) {
    int flags = (context.getProperty(UNIX_LINES).asBoolean() ? Pattern.UNIX_LINES : 0)
            | (context.getProperty(CASE_INSENSITIVE).asBoolean() ? Pattern.CASE_INSENSITIVE : 0)
            | (context.getProperty(COMMENTS).asBoolean() ? Pattern.COMMENTS : 0)
            | (context.getProperty(MULTILINE).asBoolean() ? Pattern.MULTILINE : 0)
            | (context.getProperty(LITERAL).asBoolean() ? Pattern.LITERAL : 0)
            | (context.getProperty(DOTALL).asBoolean() ? Pattern.DOTALL : 0)
            | (context.getProperty(UNICODE_CASE).asBoolean() ? Pattern.UNICODE_CASE : 0)
            | (context.getProperty(CANON_EQ).asBoolean() ? Pattern.CANON_EQ : 0)
            | (context.getProperty(UNICODE_CHARACTER_CLASS).asBoolean() ? Pattern.UNICODE_CHARACTER_CLASS : 0);
    return flags;
}

From source file:org.eclipse.mylyn.internal.web.tasks.WebRepositoryConnector.java

public static IStatus performQuery(String resource, String regexp, String taskPrefix, IProgressMonitor monitor,
        TaskDataCollector resultCollector, TaskRepository repository) {
    NamedPattern p = new NamedPattern(regexp, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL
            | Pattern.UNICODE_CASE | Pattern.CANON_EQ);

    Matcher matcher = p.matcher(resource);

    if (!matcher.find()) {
        return Status.OK_STATUS;
    } else {/*w  ww .j  ava  2 s  . c o m*/
        boolean isCorrect = true;
        do {
            if (p.getGroups().isEmpty()) {
                // "classic" mode, no named patterns
                if (matcher.groupCount() < 2) {
                    isCorrect = false;
                }
                if (matcher.groupCount() >= 1) {
                    String id = matcher.group(1);
                    String description = matcher.groupCount() > 1 ? cleanup(matcher.group(2), repository)
                            : null;
                    description = unescapeHtml(description);

                    TaskData data = createTaskData(repository, id);
                    TaskMapper mapper = new TaskMapper(data, true);
                    mapper.setCreationDate(DEFAULT_DATE);
                    mapper.setTaskUrl(taskPrefix + id);
                    mapper.setSummary(description);
                    mapper.setValue(KEY_TASK_PREFIX, taskPrefix);
                    resultCollector.accept(data);
                }
            } else {
                String id = p.group("Id", matcher); //$NON-NLS-1$
                String description = p.group("Description", matcher); //$NON-NLS-1$
                if (id == null || description == null) {
                    isCorrect = false;
                }
                if (id != null) {
                    description = unescapeHtml(description);

                    String owner = unescapeHtml(cleanup(p.group("Owner", matcher), repository)); //$NON-NLS-1$
                    String type = unescapeHtml(cleanup(p.group("Type", matcher), repository)); //$NON-NLS-1$

                    TaskData data = createTaskData(repository, id);
                    TaskMapper mapper = new TaskMapper(data, true);
                    mapper.setCreationDate(DEFAULT_DATE);
                    mapper.setTaskUrl(taskPrefix + id);
                    mapper.setSummary(description);
                    mapper.setValue(KEY_TASK_PREFIX, taskPrefix);
                    mapper.setOwner(owner);
                    mapper.setTaskKind(type);

                    String status = p.group("Status", matcher); //$NON-NLS-1$
                    if (status != null) {
                        if (COMPLETED_STATUSES.contains(status.toLowerCase())) {
                            // TODO set actual completion date here
                            mapper.setCompletionDate(DEFAULT_DATE);
                        }
                    }

                    resultCollector.accept(data);
                }
            }
        } while (matcher.find() && !monitor.isCanceled());

        if (isCorrect) {
            return Status.OK_STATUS;
        } else {
            return new Status(IStatus.ERROR, TasksWebPlugin.ID_PLUGIN, IStatus.ERROR,
                    Messages.WebRepositoryConnector_Require_two_matching_groups, null);
        }
    }
}

From source file:com.email.ReceiveEmail.java

/**
 * Strip out the emojis and symbols from the email so we can actually save
 * it in the database//ww w  .jav a2 s . c  om
 *
 * @param content String
 * @return String
 */
private static String removeEmojiAndSymbolFromString(String content) {
    String utf8tweet = "";

    if (content != null) {
        try {
            byte[] utf8Bytes = content.getBytes("UTF-8");
            utf8tweet = new String(utf8Bytes, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            ExceptionHandler.Handle(ex);
        }
        Pattern unicodeOutliers = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE);
        Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(utf8tweet);
        utf8tweet = unicodeOutlierMatcher.replaceAll(" ");
    }

    return utf8tweet;
}

From source file:org.opennms.netmgt.collectd.HttpCollector.java

private static List<HttpCollectionAttribute> processResponse(final Locale responseLocale,
        final String responseBodyAsString, final HttpCollectionSet collectionSet,
        HttpCollectionResource collectionResource) {
    LOG.debug("processResponse:");
    LOG.debug("responseBody = {}", responseBodyAsString);
    LOG.debug("getmatches = {}", collectionSet.getUriDef().getUrl().getMatches());
    List<HttpCollectionAttribute> butes = new LinkedList<HttpCollectionAttribute>();
    int flags = 0;
    if (collectionSet.getUriDef().getUrl().getCanonicalEquivalence()) {
        flags |= Pattern.CANON_EQ;
    }/*from   w ww. j a  v a  2s  .com*/
    if (collectionSet.getUriDef().getUrl().getCaseInsensitive()) {
        flags |= Pattern.CASE_INSENSITIVE;
    }
    if (collectionSet.getUriDef().getUrl().getComments()) {
        flags |= Pattern.COMMENTS;
    }
    if (collectionSet.getUriDef().getUrl().getDotall()) {
        flags |= Pattern.DOTALL;
    }
    if (collectionSet.getUriDef().getUrl().getLiteral()) {
        flags |= Pattern.LITERAL;
    }
    if (collectionSet.getUriDef().getUrl().getMultiline()) {
        flags |= Pattern.MULTILINE;
    }
    if (collectionSet.getUriDef().getUrl().getUnicodeCase()) {
        flags |= Pattern.UNICODE_CASE;
    }
    if (collectionSet.getUriDef().getUrl().getUnixLines()) {
        flags |= Pattern.UNIX_LINES;
    }
    LOG.debug("flags = {}", flags);
    Pattern p = Pattern.compile(collectionSet.getUriDef().getUrl().getMatches(), flags);
    Matcher m = p.matcher(responseBodyAsString);

    final boolean matches = m.matches();
    if (matches) {
        LOG.debug("processResponse: found matching attributes: {}", matches);
        final List<Attrib> attribDefs = collectionSet.getUriDef().getAttributes().getAttribCollection();
        final AttributeGroupType groupType = new AttributeGroupType(collectionSet.getUriDef().getName(),
                AttributeGroupType.IF_TYPE_ALL);

        final List<Locale> locales = new ArrayList<Locale>();
        if (responseLocale != null) {
            locales.add(responseLocale);
        }
        locales.add(Locale.getDefault());
        if (Locale.getDefault() != Locale.ENGLISH) {
            locales.add(Locale.ENGLISH);
        }

        for (final Attrib attribDef : attribDefs) {
            final String type = attribDef.getType();
            String value = null;
            try {
                value = m.group(attribDef.getMatchGroup());
            } catch (final IndexOutOfBoundsException e) {
                LOG.error(
                        "IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}",
                        attribDef.getMatchGroup());
                LOG.error("Regex statement: {}", collectionSet.getUriDef().getUrl().getMatches());
                continue;
            }

            if (!type.matches("^([Oo](ctet|CTET)[Ss](tring|TRING))|([Ss](tring|TRING))$")) {
                Number num = null;
                for (final Locale locale : locales) {
                    try {
                        num = NumberFormat.getNumberInstance(locale).parse(value);
                        LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
                        break;
                    } catch (final ParseException e) {
                        LOG.warn(
                                "attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.",
                                attribDef.getAlias(), locale, value);
                    }
                }

                if (num == null) {
                    LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}",
                            attribDef.getMatchGroup());
                    continue;
                }

                final HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), num);
                LOG.debug("processResponse: adding found numeric attribute: {}", bute);
                butes.add(bute);
            } else {
                HttpCollectionAttribute bute = new HttpCollectionAttribute(collectionResource,
                        new HttpCollectionAttributeType(attribDef, groupType), value);
                LOG.debug("processResponse: adding found string attribute: {}", bute);
                butes.add(bute);
            }
        }
    } else {
        LOG.debug("processResponse: found matching attributes: {}", matches);
    }
    return butes;
}

From source file:org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils.java

/**
 * /*  www.  j  a  v  a2  s.  c o  m*/
 * cli Comment method "retrieveSchemaPatternForAS400".
 * 
 * bug 12179
 */
public String retrieveSchemaPatternForAS400(String url) {
    if (url == null || "".equals(url)) {
        return null;
    }
    String libsPattern = "libraries\\s*=\\s*"; //$NON-NLS-1$
    Pattern regex = Pattern.compile(libsPattern + "(([\\w.]+?)\\s*,?\\s*)*\\s*;?", //$NON-NLS-1$
            Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE);
    Matcher regexMatcher = regex.matcher(url);
    Set<String> libs = new HashSet<String>();
    while (regexMatcher.find()) {
        String str = regexMatcher.group();
        if (str != null && !"".equals(str.trim())) { //$NON-NLS-1$
            Pattern libP = Pattern.compile(libsPattern + "(.*)"); //$NON-NLS-1$
            Matcher libMatcher = libP.matcher(str.trim());
            if (libMatcher.find()) {
                String libStr = libMatcher.group(1);
                if (libStr != null) {
                    libStr = libStr.trim();
                    if (libStr.endsWith(";")) { //$NON-NLS-1$
                        libStr = libStr.substring(0, libStr.length() - 1);
                    }
                    libStr = libStr.trim();
                    if (!libStr.equals("")) { //$NON-NLS-1$
                        String[] multiSchems = getMultiSchems(libStr);
                        if (multiSchems != null) {
                            for (String s : multiSchems) {
                                if (s != null) {
                                    libs.add(s.trim());
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if (!libs.isEmpty()) {
        StringBuffer sb = new StringBuffer();
        int index = 0;
        for (String lib : libs) {
            sb.append(lib);
            if (index < libs.size() - 1) {
                sb.append(SPLIT_CHAR);
            }
            index++;
        }
        return sb.toString();
    } else {
        return null;
    }
}

From source file:org.talend.mdm.engines.client.ui.wizards.DeployOnMDMExportWizardPage.java

/**
 * Returns the root folder name.//from w w w  .  ja  v  a 2s  . co m
 * 
 * @return
 */
private String getRootFolderName(ExportFileResource p) {
    IPath path = new Path(this.getDestinationValue(p));
    String subjectString = path.lastSegment();
    Pattern regex = Pattern.compile("(.*)(?=(\\.(tar|zip))\\b)", Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE //$NON-NLS-1$
            | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        subjectString = regexMatcher.group(0);
    }

    return subjectString.trim();
}

From source file:org.talend.repository.ui.wizards.exportjob.scriptsmanager.JobScriptsManager.java

/**
 * Returns the root folder name./*from w  ww . j a  v a2s. c  o m*/
 * 
 * @return
 */
public String getRootFolderName(String path) {
    String subjectString = new Path(path).lastSegment();
    Pattern regex = Pattern.compile("(.*)(?=(\\.(tar|zip))\\b)", Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE //$NON-NLS-1$
            | Pattern.UNICODE_CASE);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
        subjectString = regexMatcher.group(0);
    }
    return subjectString.trim();
}