Example usage for java.util.regex Matcher pattern

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

Introduction

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

Prototype

public Pattern pattern() 

Source Link

Document

Returns the pattern that is interpreted by this matcher.

Usage

From source file:org.archive.util.TextUtils.java

/**
 * Utility method using a precompiled pattern instead of using the split
 * method of the String class./*from   w  ww .j a v a  2  s . co  m*/
 * 
 * @see java.util.regex.Pattern
 * @param pattern precompiled Pattern to split by
 * @param input the character sequence to split
 * @return array of Strings split by pattern
 */
public static String[] split(String pattern, CharSequence input) {
    input = new InterruptibleCharSequence(input);
    Matcher m = getMatcher(pattern, input);
    String[] retVal = m.pattern().split(input);
    recycleMatcher(m);
    return retVal;
}

From source file:org.etudes.component.app.melete.MeleteUtil.java

public ArrayList findEmbedItemPattern(String checkforimgs) {
    ArrayList returnData = new ArrayList();
    // a and link uses href, applet uses archive, object uses data
    Pattern p1 = Pattern.compile(
            "<[iI][mM][gG]\\s|<[aA]\\s|<[eE][mM][bB][eE][dD]\\s|<[sS][cC][rR][iI][pP][tT]\\s|<[lL][iI][nN][kK]\\s|<[aA][pP][pP][lL][eE][tT]\\s|<[oO][bB][jJ][eE][cC][tT]\\s");
    Pattern pi = Pattern.compile(">|\\s[sS][rR][cC]\\s*=");
    Pattern pa = Pattern.compile(">|\\s[hH][rR][eE][fF]\\s*=");
    Pattern pa1 = Pattern.compile(">|\\s[aA][rR][cC][hH][iI][vV][eE]\\s*=");
    Pattern pd = Pattern.compile(">|\\s[dD][aA][tT][aA]\\s*=");
    Pattern ps = Pattern.compile("\\S");
    Pattern pe = Pattern.compile("\\s|>");

    int startSrc = 0;
    int endSrc = 0;
    String foundPattern = null;/*from w  ww.  j a  v  a2s.co  m*/
    while (checkforimgs != null) {
        foundPattern = null;
        // look for <img or <a
        Matcher m = p1.matcher(checkforimgs);
        if (!m.find()) // found anything?
            break;
        checkforimgs = checkforimgs.substring(m.start());
        // look for src= or href=
        if (checkforimgs.startsWith("<i") || checkforimgs.startsWith("<I") || checkforimgs.startsWith("<e")
                || checkforimgs.startsWith("<E") || checkforimgs.startsWith("<s")
                || checkforimgs.startsWith("<S"))
            m = pi.matcher(checkforimgs);
        else if (checkforimgs.startsWith("<applet") || checkforimgs.startsWith("<Applet")
                || checkforimgs.startsWith("<APPLET"))
            m = pa1.matcher(checkforimgs);
        else if (checkforimgs.startsWith("<o") || checkforimgs.startsWith("<O"))
            m = pd.matcher(checkforimgs);
        else
            m = pa.matcher(checkforimgs);

        if (m.pattern().pattern().equals(pa.pattern())) {
            if (checkforimgs.startsWith("<a") || checkforimgs.startsWith("<A"))
                foundPattern = "link";
        }
        // end = start+1 means that we found a >
        // i.e. the attribute we're looking for isn't there
        if (!m.find() || (m.end() == m.start() + 1)) {
            // prevent infinite loop by consuming the <
            checkforimgs = checkforimgs.substring(1);
            continue;
        }

        checkforimgs = checkforimgs.substring(m.end());

        // look for start of arg, a non-whitespace
        m = ps.matcher(checkforimgs);
        if (!m.find()) // found anything?
            break;

        checkforimgs = checkforimgs.substring(m.start());

        startSrc = 0;
        endSrc = 0;

        // handle either quoted or nonquoted arg
        if (checkforimgs.startsWith("\"") || checkforimgs.startsWith("\'")) {
            String quotestr = checkforimgs.substring(0, 1);
            startSrc = 1;
            endSrc = checkforimgs.indexOf(quotestr, startSrc);
            break;
        } else {
            startSrc = 0;
            // ends with whitespace or >
            m = pe.matcher(checkforimgs);
            if (!m.find()) // found anything?
                continue;
            endSrc = m.start();
        }
    } //while end

    if (foundPattern != null && foundPattern.equals("link")) {
        String anchorStr = checkforimgs.substring(startSrc, endSrc);
        anchorStr = anchorStr.trim();
        if (anchorStr != null && (anchorStr.startsWith("#") || anchorStr.startsWith("mailto:"))) {
            checkforimgs = checkforimgs.substring(endSrc);
            if (checkforimgs != null) {
                ArrayList r = findEmbedItemPattern(checkforimgs);
                checkforimgs = (String) r.get(0);
                if (r.size() > 1 && ((Integer) r.get(2)).intValue() > 0) {
                    startSrc = ((Integer) r.get(1)).intValue();
                    endSrc = ((Integer) r.get(2)).intValue();
                    foundPattern = (String) r.get(3);
                } else {
                    startSrc = 0;
                    endSrc = 0;
                }
            }
        }
    }

    returnData.add(checkforimgs);
    if (endSrc != 0) {
        returnData.add(new Integer(startSrc));
        returnData.add(new Integer(endSrc));
        returnData.add(foundPattern);
    }

    return returnData;
}

From source file:org.olat.modules.webFeed.dispatching.Path.java

/**
 * Read all parameters from the matched path.
 * //from   ww  w. j a v  a2 s.c om
 * @param match
 */
private void getParameters(final Matcher match) {
    final Pattern pattern = match.pattern();
    if (feedPattern.equals(pattern)) {
        // {feedId}/feed.rss
        type = FEED;
        feedId = Long.parseLong(match.group(1));
        // The feed file name is constant, hence not needed
    } else if (feedMediaPattern.equals(pattern)) {
        // {feedId}/icon.jpg
        type = FEED_MEDIA;
        feedId = Long.parseLong(match.group(1));
        iconFileName = match.group(2);
    } else if (itemMediaPattern.equals(pattern)) {
        // {feedId}/{itemId}/ruby.mp3
        type = ITEM_MEDIA;
        feedId = Long.parseLong(match.group(1));
        setItemId(match.group(2));
        itemFileName = match.group(3);

    } else if (authFeedPattern.equals(pattern)) {
        // {identityKey}/{token}/{feedId}/feed.rss
        type = AUTHENTICATED_FEED;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        feedId = Long.parseLong(match.group(3));
    } else if (authFeedMediaPattern.equals(pattern)) {
        // {identityKey}/{token}/{feedId}/icon.jpg
        type = AUTHENTICATED_FEED_MEDIA;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        feedId = Long.parseLong(match.group(3));
        iconFileName = match.group(4);
    } else if (authItemMediaPattern.equals(pattern)) {
        // {identityKey}/{token}/{feedId}/{itemId}/ruby.mp3
        type = AUTHENTICATED_ITEM_MEDIA;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        feedId = Long.parseLong(match.group(3));
        setItemId(match.group(4));
        itemFileName = match.group(5);

    } else if (courseFeedPattern.equals(pattern)) {
        // coursenode/{courseId}/{nodeId}/{feedId}/feed.rss
        type = COURSE_FEED;
        courseId = Long.parseLong(match.group(1));
        nodeId = match.group(2);
        feedId = Long.parseLong(match.group(3));
    } else if (courseFeedMediaPattern.equals(pattern)) {
        // coursenode/{courseId}/{nodeId}/{feedId}/icon.jpg
        type = COURSE_FEED_MEDIA;
        courseId = Long.parseLong(match.group(1));
        nodeId = match.group(2);
        feedId = Long.parseLong(match.group(3));
        iconFileName = match.group(4);
    } else if (courseItemMediaPattern.equals(pattern)) {
        // coursenode/{courseId}/{nodeId}/{feedId}/{itemId}/ruby.mp3
        type = COURSE_ITEM_MEDIA;
        courseId = Long.parseLong(match.group(1));
        nodeId = match.group(2);
        feedId = Long.parseLong(match.group(3));
        setItemId(match.group(4));
        itemFileName = match.group(5);

    } else if (authCourseFeedPattern.equals(pattern)) {
        // coursenode/{identityKey}/{token}/{courseId}/{nodeId}/{feedId}/feed.rss
        type = AUTHENTICATED_COURSE_FEED;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        courseId = Long.parseLong(match.group(3));
        nodeId = match.group(4);
        feedId = Long.parseLong(match.group(5));
    } else if (authCourseFeedMediaPattern.equals(pattern)) {
        // coursenode/{identityKey}/{token}/{courseId}/{nodeId}/{feedId}/icon.jpg
        type = AUTHENTICATED_COURSE_ICON;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        courseId = Long.parseLong(match.group(3));
        nodeId = match.group(4);
        feedId = Long.parseLong(match.group(5));
        iconFileName = match.group(6);
    } else if (authCourseItemMediaPattern.equals(pattern)) {
        // coursenode/{identityKey}/{token}/{courseId}/{nodeId}/{feedId}/{itemId}/ruby.mp3
        type = AUTHENTICATED_COURSE_ITEM;
        identityKey = Long.parseLong(match.group(1));
        token = match.group(2);
        courseId = Long.parseLong(match.group(3));
        nodeId = match.group(4);
        feedId = Long.parseLong(match.group(5));
        setItemId(match.group(6));
        itemFileName = match.group(7);
    }
}

From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java

protected void parseHexMessage(String message) {
    Matcher matcher = HEX_PATTERN.matcher(message);

    if (matcher.matches()) {
        String command = matcher.group(1);

        IrCommand theCommand = null;//from   www . j  av  a  2s. com
        for (IrCommand aCommand : irCommands) {
            if (aCommand.sequenceToHEXString().equals(command)) {
                theCommand = aCommand;
                break;
            }
        }

        if (theCommand != null) {
            for (TransceiverStatusListener listener : transceiverStatusListeners) {
                listener.onCommandReceived(this, theCommand);
            }
        } else {
            logger.error("{} does not match any know infrared command", command);
        }
    } else {
        logger.error("{} does not match the infrared message format '{}'", message, matcher.pattern());
    }
}

From source file:org.openhab.binding.irtrans.handler.EthernetBridgeHandler.java

protected void parseIRDBMessage(String message) {
    Matcher matcher = IRDB_PATTERN.matcher(message);

    if (matcher.matches()) {
        IrCommand command = new IrCommand();
        command.setRemote(matcher.group(1));
        command.setCommand(matcher.group(2));

        for (TransceiverStatusListener listener : transceiverStatusListeners) {
            listener.onCommandReceived(this, command);
        }/*from w  w  w  .j a va 2  s.  c  o  m*/
    } else {
        logger.error("{} does not match the IRDB infrared message format '{}'", message, matcher.pattern());
    }
}

From source file:org.openhab.binding.irtrans.internal.IRtransBinding.java

/**
 * Parses the rcv hex.//from   w w  w .j  a va 2  s. c  om
 * 
 * @param itemName
 *            the qualified items
 * @param message
 *            the message
 * @param ohCommand
 *            the openHAB command
 */
protected void parseHexMessage(String itemName, String message, Command ohCommand) {

    Pattern HEX_PATTERN = Pattern.compile("RCV_HEX (.*)");
    Matcher matcher = HEX_PATTERN.matcher(message);

    if (matcher.matches()) {
        String command = matcher.group(1);
        IrCommand theCommand = getIrCommand(command);

        if (theCommand != null) {
            parseDecodedCommand(itemName, theCommand, ohCommand);
        } else {
            logger.error("{} does not match any know IRtrans command", command);
        }

    } else {
        logger.error("{} does not match the IRtrans message formet ({})", message, matcher.pattern());
    }
}

From source file:org.openhab.binding.irtrans.internal.IRtransBinding.java

protected void parseIRDBMessage(String itemName, String message, Command ohCommand) {

    Pattern IRDB_PATTERN = Pattern.compile("RCV_COM (.*),(.*),(.*),(.*)");
    Matcher matcher = IRDB_PATTERN.matcher(message);

    if (matcher.matches()) {

        IrCommand theCommand = new IrCommand();
        theCommand.remote = matcher.group(1);
        theCommand.command = matcher.group(2);

        parseDecodedCommand(itemName, theCommand, ohCommand);

    } else {/*from   w w w  .j ava2  s  . com*/
        logger.error("{} does not match the IRDB IRtrans message format ({})", message, matcher.pattern());
    }
}

From source file:org.openhab.binding.irtrans.internal.IRtransGenericBindingProvider.java

/**
 * Parses the binding config.// ww w  . j  a v  a 2  s.  com
 *
 * @param config the config
 * @param item the item
 * @param bindingConfig the binding config
 * @throws BindingConfigParseException the binding config parse exception
 */
private void parseBindingConfig(IRtransBindingConfig config, Item item, String bindingConfig)
        throws BindingConfigParseException {

    String commandAsString = null;
    String host = null;
    String port = null;
    String led = null;
    String remote = null;
    String irCommand = null;
    Leds ledType = Leds.DEFAULT;

    if (bindingConfig != null) {

        Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
        Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);

        if ((!actionMatcher.matches() && !statusMatcher.matches())) {
            throw new BindingConfigParseException(getBindingType()
                    + " binding configuration must consist of five [config=" + statusMatcher.pattern()
                    + "] or six parts [config=" + actionMatcher.pattern() + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                host = actionMatcher.group(2);
                port = actionMatcher.group(3);
                led = actionMatcher.group(4);
                remote = actionMatcher.group(5);
                irCommand = actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                host = statusMatcher.group(1);
                port = statusMatcher.group(2);
                led = statusMatcher.group(3);
                remote = statusMatcher.group(4);
                irCommand = statusMatcher.group(5);
            }

            if (led.equals("*")) {
                ledType = Leds.ALL;
            } else {
                ledType = Leds.valueOf(led);
            }

            IRtransBindingConfigElement newElement = new IRtransBindingConfigElement(host, port, ledType,
                    remote, irCommand, item.getAcceptedDataTypes());

            Command command = null;
            if (commandAsString == null) {
                // for those configuration strings that are not really linked to a openHAB command we
                // create a dummy Command to be able to store the configuration information
                // I have choosen to do that with NumberItems
                NumberItem dummy = new NumberItem(Integer.toString(counter));
                command = createCommandFromString(dummy, Integer.toString(counter));
                counter++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }

            config.put(command, newElement);

        }
    } else
        return;

}

From source file:org.opennms.netmgt.mib2events.Mib2Events.java

public static Matcher getMatcherForOid(String trapOid) {
    Matcher m = TRAP_OID_PATTERN.matcher(trapOid);
    if (!m.matches()) {
        throw new IllegalStateException(
                "Could not match the trap OID '" + trapOid + "' against '" + m.pattern().pattern() + "'");
    }/*  w  ww.  j ava 2 s .  c  om*/
    return m;
}

From source file:org.pentaho.platform.plugin.services.importexport.PentahoMetadataFileInfo.java

public PentahoMetadataFileInfo(final String path) {
    this.path = path;
    this.fileType = UNKNOWN;
    final String internalPath = RepositoryFilenameUtils.normalize(path, true);
    if (!StringUtils.isEmpty(internalPath)) {
        final String fileExtension = RepositoryFilenameUtils.getExtension(path);
        if (StringUtils.equals(fileExtension, "xmi")) {
            for (final Pattern xmiPattern : xmiPatterns) {
                final Matcher xmiMatcher = xmiPattern.matcher(internalPath);
                if (xmiMatcher.matches()) {
                    log.trace("MATCH: [" + internalPath + "] by [" + xmiPattern.pattern() + "] - group(s)=["
                            + xmiMatcher.group(1) + "]");
                    initialize(FileType.XMI, xmiMatcher.group(1), null);
                    break;
                }//from   ww w.  j a va 2 s .  c  om
            }
        } else if (StringUtils.equals(fileExtension, "properties")) {
            for (final Pattern propertyBundlePattern : propertyBundlePatternsNoPostfix) {
                final Matcher propertyBundleMatcher = propertyBundlePattern.matcher(internalPath);
                if (propertyBundleMatcher.matches()) {
                    log.trace("MATCH: [" + internalPath + "] by [" + propertyBundleMatcher.pattern()
                            + "] - group(s)=[" + propertyBundleMatcher.group(1) + " : "
                            + propertyBundleMatcher.group(2) + "]");
                    initialize(FileType.PROPERTIES, propertyBundleMatcher.group(1),
                            propertyBundleMatcher.group(2));
                    break;
                }
            }
            for (final Pattern propertyBundlePattern : propertyBundlePatternsWithPostfix) {
                final Matcher propertyBundleMatcher = propertyBundlePattern.matcher(internalPath);
                if (propertyBundleMatcher.matches()) {
                    log.trace("MATCH: [" + internalPath + "] by [" + propertyBundleMatcher.pattern()
                            + "] - group(s)=[" + propertyBundleMatcher.group(1) + " : "
                            + propertyBundleMatcher.group(2) + "]");
                    initialize(FileType.PROPERTIES,
                            propertyBundleMatcher.group(1) + PROPERTY_BUNDLE_DOMAIN_POSTFIX,
                            propertyBundleMatcher.group(2));
                    break;
                }
            }
        }
    }
}