Search in sources :

Example 41 with MatchResult

use of java.util.regex.MatchResult in project nifi by apache.

the class SyslogParser method parseEvent.

/**
 * Parses a SyslogEvent from a byte array.
 *
 * @param bytes a byte array containing a syslog message
 * @param sender the hostname of the syslog server that sent the message
 * @return a SyslogEvent parsed from the byte array
 */
public SyslogEvent parseEvent(final byte[] bytes, final String sender) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }
    // remove trailing new line before parsing
    int length = bytes.length;
    if (bytes[length - 1] == '\n') {
        length = length - 1;
    }
    final String message = new String(bytes, 0, length, charset);
    final SyslogEvent.Builder builder = new SyslogEvent.Builder().valid(false).fullMessage(message).rawMessage(bytes).sender(sender);
    for (Pattern pattern : MESSAGE_PATTERNS) {
        final Matcher matcher = pattern.matcher(message);
        if (!matcher.matches()) {
            continue;
        }
        final MatchResult res = matcher.toMatchResult();
        for (int grp = 1; grp <= res.groupCount(); grp++) {
            String value = res.group(grp);
            if (grp == SYSLOG_TIMESTAMP_POS) {
                builder.timestamp(value);
            } else if (grp == SYSLOG_HOSTNAME_POS) {
                builder.hostname(value);
            } else if (grp == SYSLOG_PRIORITY_POS) {
                int pri = Integer.parseInt(value);
                int sev = pri % 8;
                int facility = pri / 8;
                builder.priority(value);
                builder.severity(String.valueOf(sev));
                builder.facility(String.valueOf(facility));
            } else if (grp == SYSLOG_VERSION_POS) {
                builder.version(value);
            } else if (grp == SYSLOG_BODY_POS) {
                builder.msgBody(value);
            }
        }
        builder.valid(true);
        break;
    }
    // either invalid w/original msg, or fully parsed event
    return builder.build();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Example 42 with MatchResult

use of java.util.regex.MatchResult in project Wurst-MC-1.12 by Wurst-Imperium.

the class JGoogleAnalyticsTracker method setProxy.

/**
 * Define the proxy to use for all GA tracking requests.
 * <p>
 * Call this static method early (before creating any tracking requests).
 *
 * @param proxyAddr
 *            "addr:port" of the proxy to use; may also be given as URL
 *            ("http://addr:port/").
 */
public static void setProxy(String proxyAddr) {
    if (proxyAddr != null) {
        Scanner s = new Scanner(proxyAddr);
        // Split into "proxyAddr:proxyPort".
        proxyAddr = null;
        int proxyPort = 8080;
        try {
            s.findInLine("(http://|)([^:/]+)(:|)([0-9]*)(/|)");
            MatchResult m = s.match();
            if (m.groupCount() >= 2)
                proxyAddr = m.group(2);
            if (m.groupCount() >= 4 && !(m.group(4).length() == 0))
                proxyPort = Integer.parseInt(m.group(4));
        } finally {
            s.close();
        }
        if (proxyAddr != null) {
            SocketAddress sa = new InetSocketAddress(proxyAddr, proxyPort);
            setProxy(new Proxy(Type.HTTP, sa));
        }
    }
}
Also used : Scanner(java.util.Scanner) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) MatchResult(java.util.regex.MatchResult)

Example 43 with MatchResult

use of java.util.regex.MatchResult in project HolandaCatalinaFw by javaito.

the class Query method compile.

/**
 * Create a query instance from sql definition.
 * @param groups
 * @param startGroup
 * @return Query instance.
 */
private static Query compile(List<String> groups, List<String> richTexts, Integer startGroup) {
    Query query;
    Pattern pattern = SystemProperties.getPattern(SystemProperties.Query.SELECT_REGULAR_EXPRESSION);
    Matcher matcher = pattern.matcher(groups.get(startGroup));
    if (matcher.matches()) {
        String selectBody = matcher.group(SystemProperties.getInteger(SystemProperties.Query.SELECT_GROUP_INDEX));
        selectBody = selectBody.replaceFirst(Strings.CASE_INSENSITIVE_REGEX_FLAG + SystemProperties.get(SystemProperties.Query.ReservedWord.SELECT), Strings.EMPTY_STRING);
        String fromBody = matcher.group(SystemProperties.getInteger(SystemProperties.Query.FROM_GROUP_INDEX));
        fromBody = fromBody.replaceFirst(Strings.CASE_INSENSITIVE_REGEX_FLAG + SystemProperties.get(SystemProperties.Query.ReservedWord.FROM), Strings.EMPTY_STRING);
        String conditionalBody = matcher.group(SystemProperties.getInteger(SystemProperties.Query.CONDITIONAL_GROUP_INDEX));
        if (conditionalBody != null && conditionalBody.endsWith(SystemProperties.get(SystemProperties.Query.ReservedWord.STATEMENT_END))) {
            conditionalBody = conditionalBody.substring(0, conditionalBody.indexOf(SystemProperties.get(SystemProperties.Query.ReservedWord.STATEMENT_END)) - 1);
        }
        query = new Query(fromBody.trim());
        for (String returnField : selectBody.split(SystemProperties.get(SystemProperties.Query.ReservedWord.ARGUMENT_SEPARATOR))) {
            query.addReturnField((QueryReturnParameter) processStringValue(groups, richTexts, returnField, null, QueryReturnParameter.class));
        }
        if (conditionalBody != null) {
            Pattern conditionalPatter = SystemProperties.getPattern(SystemProperties.Query.CONDITIONAL_REGULAR_EXPRESSION, Pattern.CASE_INSENSITIVE);
            String[] conditionalElements = conditionalPatter.split(conditionalBody);
            String element;
            String elementValue;
            for (int i = 0; i < conditionalElements.length; i++) {
                element = conditionalElements[i++].trim();
                elementValue = conditionalElements[i].trim();
                if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.JOIN)) || element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.INNER)) || element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.LEFT)) || element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.RIGHT))) {
                    Join.JoinType type = Join.JoinType.valueOf(element.toUpperCase());
                    if (type != Join.JoinType.JOIN) {
                        elementValue = conditionalElements[++i].trim();
                    }
                    String[] joinElements = elementValue.split(SystemProperties.get(SystemProperties.Query.JOIN_REGULAR_EXPRESSION));
                    String joinResource = joinElements[SystemProperties.getInteger(SystemProperties.Query.JOIN_RESOURCE_NAME_INDEX)].trim();
                    String joinEvaluators = joinElements[SystemProperties.getInteger(SystemProperties.Query.JOIN_EVALUATORS_INDEX)].trim();
                    if (joinEvaluators.startsWith(Strings.REPLACEABLE_GROUP)) {
                        joinEvaluators = groups.get(Integer.parseInt(joinEvaluators.replace(Strings.REPLACEABLE_GROUP, Strings.EMPTY_STRING)));
                    }
                    Join join = new Join(query, joinResource, type);
                    completeEvaluatorCollection(joinEvaluators, groups, richTexts, join, 0, new AtomicInteger(0));
                    query.addJoin(join);
                } else if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.WHERE))) {
                    completeEvaluatorCollection(elementValue, groups, richTexts, query, 0, new AtomicInteger(0));
                } else if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.ORDER_BY))) {
                    for (String orderField : elementValue.split(SystemProperties.get(SystemProperties.Query.ReservedWord.ARGUMENT_SEPARATOR))) {
                        query.addOrderField((QueryOrderParameter) processStringValue(groups, richTexts, orderField, null, QueryOrderParameter.class));
                    }
                } else if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.GROUP_BY))) {
                    for (String orderField : elementValue.split(SystemProperties.get(SystemProperties.Query.ReservedWord.ARGUMENT_SEPARATOR))) {
                        query.addGroupField((QueryReturnParameter) processStringValue(groups, richTexts, orderField, null, QueryReturnParameter.class));
                    }
                } else if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.LIMIT))) {
                    query.setLimit(Integer.parseInt(elementValue));
                } else if (element.equalsIgnoreCase(SystemProperties.get(SystemProperties.Query.ReservedWord.START))) {
                    query.setStart(Integer.parseInt(elementValue));
                }
            }
        }
    } else {
        MatchResult mr = matcher.toMatchResult();
        throw new IllegalArgumentException();
    }
    return query;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MatchResult(java.util.regex.MatchResult)

Example 44 with MatchResult

use of java.util.regex.MatchResult in project openchemlib by Actelion.

the class StringFunctions method matchFirst.

public static final Point matchFirst(String str, String regex) {
    Pattern pa = Pattern.compile(regex);
    Matcher ma = pa.matcher(str);
    Point p = null;
    if (ma.find()) {
        MatchResult mr = ma.toMatchResult();
        int start = mr.start();
        int end = mr.end();
        p = new Point(start, end);
    }
    return p;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Point(java.awt.Point) MatchResult(java.util.regex.MatchResult) Point(java.awt.Point)

Example 45 with MatchResult

use of java.util.regex.MatchResult in project openchemlib by Actelion.

the class StringFunctions method getStringFromRegEx.

/**
 * @param str
 * @param regex
 * @return null if substring not found.
 */
public static String getStringFromRegEx(String str, String regex) {
    Pattern pa = Pattern.compile(regex);
    Matcher ma = pa.matcher(str);
    int start = -1;
    int end = -1;
    if (ma.find()) {
        MatchResult mr = ma.toMatchResult();
        start = mr.start();
        end = mr.end();
    } else {
        return null;
    }
    return str.substring(start, end);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult) Point(java.awt.Point)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1