Package org.jregex

Examples of org.jregex.Pattern


                @Override
                public Object activate(IokeObject self, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
                    getArguments().getEvaluatedArguments(context, message, on, args, new HashMap<String, Object>());
                    String real = Text.getText(on);
                    List<Object> r = new ArrayList<Object>();
                    Pattern p = null;

                    if(args.size() == 0) {
                        p = new Pattern("\\s");
                    } else {
                        Object arg = args.get(0);
                        if(IokeObject.data(arg) instanceof Regexp) {
                            p = Regexp.getRegexp(arg);
                        } else {
                            String around = Text.getText(arg);
                            p = new Pattern(Pattern.quote(around));
                        }
                    }

                    RETokenizer tok = new RETokenizer(p, real);
                    tok.setEmptyEnabled(false);
                    while(tok.hasMore()) {
                        r.add(context.runtime.newText(tok.nextToken()));
                    }

                    return context.runtime.newList(r);
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Takes two text arguments where the first is the substring to replace, and the second is the replacement to insert. Will only replace the first match, if any is found, and return a new Text with the result.", new TypeCheckingNativeMethod("replace") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withRequiredPositional("pattern")
                    .withRequiredPositional("replacement")
                    .getArguments();

                @Override
                public TypeCheckingArgumentsDefinition getArguments() {
                    return ARGUMENTS;
                }

                @Override
                public Object activate(IokeObject self, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
                    getArguments().getEvaluatedArguments(context, message, on, args, new HashMap<String, Object>());
                    String initial = Text.getText(on);
                    String repl = Text.getText(args.get(1));

                    Object arg = args.get(0);

                    Pattern pat = null;
                    if(IokeObject.data(arg) instanceof Regexp) {
                        pat = Regexp.getRegexp(arg);
                    } else {
                        String around = Text.getText(arg);
                        pat = new Pattern(Pattern.quote(around));
                    }

                    Replacer r = pat.replacer(repl);
                    String result = r.replaceFirst(initial);

                    return context.runtime.newText(result);
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Takes two text arguments where the first is the substring to replace, and the second is the replacement to insert. Will replace all matches, if any is found, and return a new Text with the result.", new TypeCheckingNativeMethod("replaceAll") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withRequiredPositional("pattern")
                    .withRequiredPositional("replacement")
                    .getArguments();

                @Override
                public TypeCheckingArgumentsDefinition getArguments() {
                    return ARGUMENTS;
                }

                @Override
                public Object activate(IokeObject self, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
                    getArguments().getEvaluatedArguments(context, message, on, args, new HashMap<String, Object>());
                    String initial = Text.getText(on);
                    String repl = Text.getText(args.get(1));

                    Object arg = args.get(0);

                    Pattern pat = null;
                    if(IokeObject.data(arg) instanceof Regexp) {
                        pat = Regexp.getRegexp(arg);
                    } else {
                        String around = Text.getText(arg);
                        pat = new Pattern(Pattern.quote(around));
                    }

                    Replacer r = pat.replacer(repl);
                    String result = r.replace(initial);

                    return context.runtime.newText(result);
                }
            }));
View Full Code Here


public abstract class AbstractBench {
    protected void bench(String _reg, String _str, int warmup, int times) throws Exception {
        String reg = _reg;
        char[] str = _str.toCharArray();

        Pattern p = new Pattern(reg);

        System.err.println("::: /" + _reg + "/ =~ \"" + _str + "\", " + warmup + " * " + times + " times");

        for(int j=0;j<warmup;j++) {
            long before = System.currentTimeMillis();
            for(int i = 0; i < times; i++) {
                p.matcher(str,0,str.length).find();
            }
            long time = System.currentTimeMillis() - before;
            System.err.println(":  " + time + "ms");
        }
    }
View Full Code Here

    protected void benchBestOf(String _reg, String _str, int warmup, int times) throws Exception {
        String reg = _reg;
        char[] str = _str.toCharArray();

        Pattern p = new Pattern(reg);

        System.err.println("::: /" + _reg + "/ =~ \"" + _str + "\", " + warmup + " * " + times + " times");

        long best = Long.MAX_VALUE;

        for(int j=0;j<warmup;j++) {
            long before = System.currentTimeMillis();
            for(int i = 0; i < times; i++) {
                p.matcher(str,0,str.length).find();
            }
            long time = System.currentTimeMillis() - before;
            if(time < best) {
                best = time;
            }
View Full Code Here

        this.flags = flags;
    }

    public static Regexp create(String pattern, String flags, IokeObject context, IokeObject message) throws ControlFlow {
        try {
            return new Regexp(pattern, new Pattern(pattern, flags), flags);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
View Full Code Here

        }
    }

    static Regexp create(String pattern, String flags) {
        try {
            return new Regexp(pattern, new Pattern(pattern, flags), flags);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }
View Full Code Here

        this.regexp = regexp;
        this.flags = flags;
    }

    public static Regexp create(String pattern, String flags) {
        return new Regexp(pattern, new Pattern(pattern, flags), flags);
    }
View Full Code Here

TOP

Related Classes of org.jregex.Pattern

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.