本文整理汇总了Java中dk.brics.automaton.RegExp类的典型用法代码示例。如果您正苦于以下问题:Java RegExp类的具体用法?Java RegExp怎么用?Java RegExp使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RegExp类属于dk.brics.automaton包,在下文中一共展示了RegExp类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: modAutomata
import dk.brics.automaton.RegExp; //导入依赖的package包/类
private boolean modAutomata(String rexp1, String rexp2, Op op) {
Automaton a = new RegExp(rexp1).toAutomaton();
Automaton b = new RegExp(rexp2).toAutomaton();
Automaton c = null;
switch(op) {
case CONCAT:
c = a.concatenate(b);
break;
case UNION:
c = a.union(b);
break;
case ISECT:
c = a.intersection(b);
break;
}
Assert.assertNotNull(c);
return compareRexpAutomaton(c);
}
示例2: languageTagAutomaton
import dk.brics.automaton.RegExp; //导入依赖的package包/类
protected static Automaton languageTagAutomaton() {
return new RegExp(
"("+
"([a-zA-Z]{2,3}"+
"("+
"(-[a-zA-Z]{3}){0,3}"+ // extlang
")?"+
")|"+
"[a-zA-Z]{4}|"+ // 4ALPHA
"[a-zA-Z]{5,8}"+ // 5*8ALPHA
")"+ // language
"(-[a-zA-Z]{4})?"+ // script
"(-([a-zA-Z]{2}|[0-9]{3}))?"+ // region
"(-([a-zA-Z0-9]{5,8}|([0-9][a-z0-9]{3})))*"+ // variant
"(-([a-wy-zA-WY-Z0-9](-[a-zA-Z0-9]{2,8})+))*"+ // extension
"(-x(-[a-zA-Z0-9]{1,8})+)?" // privateuse
).toAutomaton();
}
示例3: RegexListSearcher
import dk.brics.automaton.RegExp; //导入依赖的package包/类
public RegexListSearcher(String re) {
if (re.startsWith("^")) {
re = re.substring(1);
}
if (re.endsWith("$") && !re.endsWith("\\$")) {
re = re.substring(0, re.length() - 1);
}
Automaton automaton = new RegExp(re).toAutomaton();
prefixBegin = automaton.getCommonPrefix();
prefixLen = prefixBegin.length();
if (0 < prefixLen) {
char max = Chars.checkedCast(prefixBegin.charAt(prefixLen - 1) + 1);
prefixEnd = prefixBegin.substring(0, prefixLen - 1) + max;
prefixOnly = re.equals(prefixBegin + ".*");
} else {
prefixEnd = "";
prefixOnly = false;
}
pattern = prefixOnly ? null : new RunAutomaton(automaton);
}
示例4: compareRexpAutomaton
import dk.brics.automaton.RegExp; //导入依赖的package包/类
private boolean compareRexpAutomaton(Automaton a0) {
LOGGER.debug("old Automaton:" + a0.toDot());
LOGGER.debug("is det" + a0.isDeterministic());
String s0 = Autorex.getRegexFromAutomaton(a0);
LOGGER.debug("Regexp 1 " + s0);
RegExp r0new = new RegExp(s0);
Automaton a0new = r0new.toAutomaton();
LOGGER.debug("new Automaton:" + a0new.toDot());
return a0new.equals(a0);
}
示例5: testTransformation
import dk.brics.automaton.RegExp; //导入依赖的package包/类
@Test
public void testTransformation() {
Automaton a = new RegExp("(abc)+[0-9]{1,3}[dg]*").toAutomaton();
Automaton b = new RegExp("12345678").toAutomaton();
Automaton c = new RegExp(".{0,5}").toAutomaton();
Automaton d = a.union(b).intersection(c);
String s0 = Autorex.getRegexFromAutomaton(d);
LOGGER.debug("Regex String: {}", s0);
RegExp r0new = new RegExp(s0);
Automaton a0new = r0new.toAutomaton();
Assert.assertTrue(a0new.equals(d));
}
示例6: testConversion
import dk.brics.automaton.RegExp; //导入依赖的package包/类
@Test
public void testConversion() {
String s = "hello my name is Alice";
Automaton a = new RegExp(s).toAutomaton();
Automaton substr = Autorex.getSubstringAutomaton(a);
Automaton sfx = Autorex.getSuffixAutomaton(a);
Automaton ccas = Autorex.getCamelCaseAutomaton(a);
Assert.assertNotNull(substr);
Assert.assertNotNull(ccas);
Assert.assertNotNull(sfx);
Assert.assertNotNull(substr.toDot());
Assert.assertNotNull(ccas.toDot());
Assert.assertNotNull(sfx.toDot());
for( int idx = 0 ; idx < s.length() ; idx++ ) {
for( int nidx = 1 ; nidx <= s.length() - idx ; nidx++ ) {
String sub = s.substring(idx, idx+nidx);
Assert.assertTrue(substr.run(sub));
if(!s.endsWith(sub)) {
Assert.assertFalse(sfx.run(sub));
} else {
Assert.assertTrue(sfx.run(sub));
}
}
}
for( int idx = 0 ; idx < s.length() ; idx++ ) {
String suf = s.substring(idx);
Assert.assertTrue(sfx.run(suf));
}
Assert.assertTrue(ccas.run(s.toUpperCase()));
}
示例7: createAutomaton
import dk.brics.automaton.RegExp; //导入依赖的package包/类
private static Automata createAutomaton(List<String> patterns) {
final List<Automaton> automata = new ArrayList<>();
for (String ptn: patterns) {
try {
Automaton automaton = new RegExp(ptn, FLAGS).toAutomaton();
automaton.minimize();
automata.add(automaton);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid regexp, "+e.getMessage()+", source: "+_printablePattern(ptn));
}
}
return Automata.construct(automata);
}
示例8: testRandRegExp
import dk.brics.automaton.RegExp; //导入依赖的package包/类
@Test
public void testRandRegExp() {
for (int runs = 0; runs < 20; runs++) {
Automaton a = new Automaton();
Set<String> finiteStringSet = new HashSet<String>();
for (int i = 0; i < 10; i++) {
String s = genRandString();
finiteStringSet.add(s);
Automaton sa = new RegExp(s).toAutomaton();
a = a.union(sa);
}
String input = genRandString();
String mins = finiteStringSet.iterator().next();
double min = (double) Math.abs(StringUtils.getLevenshteinDistance(input, finiteStringSet.iterator().next()));
// find min levenshtein distance
for (String fs : finiteStringSet) {
int ld = Math.abs(StringUtils.getLevenshteinDistance(input, fs));
if (ld < min) {
min = ld;
mins = fs;
}
}
LOGGER.info("Min ld is: " + min + " input:" + input + " " + mins);
Prex am = new Prex(a, 1.0, 1.0, 1.0);
double cost = am.evaluateCost(input, false, false);
Assert.assertTrue(cost == min);
}
}
示例9: matchesBrics
import dk.brics.automaton.RegExp; //导入依赖的package包/类
@NotNull
public StringPattern matchesBrics(@NonNls @NotNull 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("<whitespacechar>");
}
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(@NotNull 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);
}
});
}
示例10: internalCompile
import dk.brics.automaton.RegExp; //导入依赖的package包/类
private Automaton internalCompile() {
Automaton am = m_automaton;
if (am == null) {
RegExp re = new RegExp(getRegexp());
am = re.toAutomaton(true);
am.minimize();
m_automaton = am;
}
return am;
}
示例11: main
import dk.brics.automaton.RegExp; //导入依赖的package包/类
/**
* @param args
*/
public static void main(String[] args) {
RegExp regExp = new RegExp(PATTERN);
Pattern pattern = Pattern.compile(PATTERN);
Automaton automaton = regExp.toAutomaton();
RunAutomaton runAutomaton = new RunAutomaton(automaton);
long start = System.currentTimeMillis();
for (int i = 0; i < NUMBER_LOOPS; i++) {
runAutomaton.run(string);
}
System.out.println("Run automaton : " + (System.currentTimeMillis() - start));
start = System.currentTimeMillis();
for (int i = 0; i < NUMBER_LOOPS; i++) {
Matcher matcher = pattern.matcher(string);
matcher.matches();
}
System.out.println("Pattern : " + (System.currentTimeMillis() - start));
}
示例12: create
import dk.brics.automaton.RegExp; //导入依赖的package包/类
@Override
public Regex create(String pattern) {
RegExp regexpr = new RegExp(pattern);
Automaton auto = regexpr.toAutomaton();
RunAutomaton runauto = new RunAutomaton(auto, true);
return new Regex() {
@Override
public boolean containsMatch(String string) {
return runauto.run(string);
}
};
}
示例13: main
import dk.brics.automaton.RegExp; //导入依赖的package包/类
public static void main(String[] args) {
RegExp r = new RegExp("\"hello\" \"world\"", RegExp.ALL);
Automaton a = r.toAutomaton();
a.minimize();
a.prefixClose();
System.out.println(a.toDot());
}
示例14: getRegexpFormulaString
import dk.brics.automaton.RegExp; //导入依赖的package包/类
public static Constraint getRegexpFormulaString(String regexp, String prefix, int length) {
RegExp r = new RegExp(regexp);
Automaton a = r.toAutomaton();
length--;
if (length == -1) {
if (a.getInitialState().isAccept()) {
return SymbolicTrueConstraint.instance;
} else {
return SymbolicFalseConstraint.instance;
}
} else {
return createFormula(a, prefix, length);
}
}
示例15: getLengthFormulaString
import dk.brics.automaton.RegExp; //导入依赖的package包/类
public static Constraint getLengthFormulaString(
String regexp, String prefix, int sym, boolean accept) {
RegExp r = new RegExp(regexp);
Automaton a = r.toAutomaton();
String example = a.getShortestExample(accept);
if (example != null)
return intCompare(
prefix, sym, example.length(), COMPARISON_OPS.GE);
else return intCompare(prefix, sym, 0, COMPARISON_OPS.GE);
}