Java Code Examples for dk.brics.automaton.RunAutomaton

The following examples show how to use dk.brics.automaton.RunAutomaton. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: multiregexp   Source File: UnicodeTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testAutomatonWithUnicode() {
    final RegExp regexp = new RegExp("([0-9]{2,4}年)?[0-9]{1,2}月[0-9]{1,2}日");
    final Automaton forwardAutomaton = regexp.toAutomaton();
    {
        final RunAutomaton runAutomaton = new RunAutomaton(forwardAutomaton);
        Assert.assertTrue(runAutomaton.run("1982年9月17日"));
        Assert.assertFalse(runAutomaton.run("1982年9月127日"));
    }
}
 
Example 2
Source Project: consulo   Source File: StringPattern.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public StringPattern matchesBrics(@NonNls @Nonnull final String s) {
  final String escaped = StringUtil.escapeToRegexp(s);
  if (escaped.equals(s)) {
    return equalTo(s);
  }

  StringBuilder sb = new StringBuilder(s.length()*5);
  for (int i = 0; i < s.length(); i++) {
    final char c = s.charAt(i);
    if(c == ' ') {
      sb.append("<whitespace>");
    }
    else
    //This is really stupid and inconvenient builder - it breaks any normal pattern with uppercase
    if(Character.isUpperCase(c)) {
      sb.append('[').append(Character.toUpperCase(c)).append(Character.toLowerCase(c)).append(']');
    }
    else
    {
      sb.append(c);
    }
  }
  final RegExp regExp = new RegExp(sb.toString());
  final Automaton automaton = regExp.toAutomaton(new DatatypesAutomatonProvider());
  final RunAutomaton runAutomaton = new RunAutomaton(automaton, true);

  return with(new ValuePatternCondition<String>("matchesBrics") {
    @Override
    public boolean accepts(@Nonnull String str, final ProcessingContext context) {
      if (!str.isEmpty() && (str.charAt(0) == '"' || str.charAt(0) == '\'')) str = str.substring(1);
      return runAutomaton.run(str);
    }

    @Override
    public Collection<String> getValues() {
      return Collections.singleton(s);
    }
  });
}
 
Example 3
Source Project: anthelion   Source File: AutomatonURLFilter.java    License: Apache License 2.0 4 votes vote down vote up
Rule(boolean sign, String regex) {
  super(sign, regex);
  automaton = new RunAutomaton(new RegExp(regex, RegExp.ALL).toAutomaton());
}
 
Example 4
Source Project: spork   Source File: CompiledAutomaton.java    License: Apache License 2.0 4 votes vote down vote up
public CompiledAutomaton( String rhsPattern ) {
    RegExp regexpr = new dk.brics.automaton.RegExp(rhsPattern, RegExp.NONE);
    Automaton auto = regexpr.toAutomaton();
    this.runauto = new RunAutomaton(auto, true);
}
 
Example 5
Source Project: nutch-htmlunit   Source File: AutomatonURLFilter.java    License: Apache License 2.0 4 votes vote down vote up
Rule(boolean sign, String regex) {
  super(sign, regex);
  automaton = new RunAutomaton(new RegExp(regex, RegExp.ALL).toAutomaton());
}