use of java.util.regex.PatternSyntaxException in project intellij-community by JetBrains.
the class SearchResults method findInRange.
private void findInRange(TextRange r, Editor editor, FindModel findModel, ArrayList<FindResult> results) {
VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
CharSequence charSequence = editor.getDocument().getCharsSequence();
int offset = r.getStartOffset();
int maxOffset = Math.min(r.getEndOffset(), charSequence.length());
FindManager findManager = FindManager.getInstance(getProject());
while (true) {
FindResult result;
try {
CharSequence bombedCharSequence = StringUtil.newBombedCharSequence(charSequence, 3000);
result = findManager.findString(bombedCharSequence, offset, findModel, virtualFile);
} catch (PatternSyntaxException | ProcessCanceledException e) {
result = null;
}
if (result == null || !result.isStringFound())
break;
int newOffset = result.getEndOffset();
if (result.getEndOffset() > maxOffset)
break;
if (offset == newOffset) {
if (offset < maxOffset - 1) {
offset++;
} else {
results.add(result);
break;
}
} else {
offset = newOffset;
}
results.add(result);
}
}
use of java.util.regex.PatternSyntaxException in project robovm by robovm.
the class Pattern2Test method testEscapes.
public void testEscapes() throws PatternSyntaxException {
Pattern p;
Matcher m;
// Test \\ sequence
p = Pattern.compile("([a-z]+)\\\\([a-z]+);");
m = p.matcher("fred\\ginger;abbott\\costello;jekell\\hyde;");
assertTrue(m.find());
assertEquals("fred", m.group(1));
assertEquals("ginger", m.group(2));
assertTrue(m.find());
assertEquals("abbott", m.group(1));
assertEquals("costello", m.group(2));
assertTrue(m.find());
assertEquals("jekell", m.group(1));
assertEquals("hyde", m.group(2));
assertFalse(m.find());
// Test \n, \t, \r, \f, \e, \a sequences
p = Pattern.compile("([a-z]+)[\\n\\t\\r\\f\\e\\a]+([a-z]+)");
m = p.matcher("aa\nbb;cc \rdd;eeff;gg\nhh");
assertTrue(m.find());
assertEquals("aa", m.group(1));
assertEquals("bb", m.group(2));
assertTrue(m.find());
assertEquals("cc", m.group(1));
assertEquals("dd", m.group(2));
assertTrue(m.find());
assertEquals("ee", m.group(1));
assertEquals("ff", m.group(2));
assertTrue(m.find());
assertEquals("gg", m.group(1));
assertEquals("hh", m.group(2));
assertFalse(m.find());
// Test \\u and \\x sequences
p = Pattern.compile("([0-9]+)[\\u0020:\\x21];");
m = p.matcher("11:;22 ;33-;44!;");
assertTrue(m.find());
assertEquals("11", m.group(1));
assertTrue(m.find());
assertEquals("22", m.group(1));
assertTrue(m.find());
assertEquals("44", m.group(1));
assertFalse(m.find());
// Test invalid unicode sequences
try {
p = Pattern.compile("\\u");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\u;");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\u002");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\u002;");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
// Test invalid hex sequences
try {
p = Pattern.compile("\\x");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\x;");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
// icu4c allows 1 to 6 hex digits in \x escapes.
p = Pattern.compile("\\xa");
p = Pattern.compile("\\xab");
p = Pattern.compile("\\xabc");
p = Pattern.compile("\\xabcd");
p = Pattern.compile("\\xabcde");
p = Pattern.compile("\\xabcdef");
// (Further digits would just be treated as characters after the escape.)
try {
p = Pattern.compile("\\xg");
fail();
} catch (PatternSyntaxException expected) {
}
// Test \0 (octal) sequences (1, 2 and 3 digit)
p = Pattern.compile("([0-9]+)[\\07\\040\\0160];");
m = p.matcher("11;22:;33 ;44p;");
assertTrue(m.find());
assertEquals("11", m.group(1));
assertTrue(m.find());
assertEquals("33", m.group(1));
assertTrue(m.find());
assertEquals("44", m.group(1));
assertFalse(m.find());
// Test invalid octal sequences
try {
p = Pattern.compile("\\08");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\0");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
try {
p = Pattern.compile("\\0;");
fail("PatternSyntaxException expected");
} catch (PatternSyntaxException e) {
}
// Test \c (control character) sequence
p = Pattern.compile("([0-9]+)[\\cA\\cB\\cC\\cD];");
m = p.matcher("11;22:;33;44p;55;66;");
assertTrue(m.find());
assertEquals("11", m.group(1));
assertTrue(m.find());
assertEquals("33", m.group(1));
assertTrue(m.find());
assertEquals("55", m.group(1));
assertTrue(m.find());
assertEquals("66", m.group(1));
assertFalse(m.find());
// More thorough control escape test
// Ensure that each escape matches exactly the corresponding
// character
// code and no others (well, from 0-255 at least)
int i, j;
for (i = 0; i < 26; i++) {
p = Pattern.compile("\\c" + Character.toString((char) ('A' + i)));
int match_char = -1;
for (j = 0; j < 255; j++) {
m = p.matcher(Character.toString((char) j));
if (m.matches()) {
assertEquals(-1, match_char);
match_char = j;
}
}
assertTrue(match_char == i + 1);
}
// Test invalid control escapes
// icu4c 50 accepts this pattern, and treats it as a literal.
//try {
p = Pattern.compile("\\c");
assertTrue(p.matcher("x\\cy").find());
// fail(p.matcher("").toString());
//} catch (PatternSyntaxException e) {
//}
// But \cH works.
p = Pattern.compile("\\cH");
assertTrue(p.matcher("xy").find());
assertFalse(p.matcher("x\\cHy").find());
// originally contributed test did not check the result
// TODO: check what RI does here
// try {
// p = Pattern.compile("\\c;");
// fail("PatternSyntaxException expected");
// } catch (PatternSyntaxException e) {
// }
//
// try {
// p = Pattern.compile("\\ca;");
// fail("PatternSyntaxException expected");
// } catch (PatternSyntaxException e) {
// }
//
// try {
// p = Pattern.compile("\\c4;");
// fail("PatternSyntaxException expected");
// } catch (PatternSyntaxException e) {
// }
}
use of java.util.regex.PatternSyntaxException in project spring-framework by spring-projects.
the class OperatorMatches method getValueInternal.
/**
* Check the first operand matches the regex specified as the second operand.
* @param state the expression state
* @return {@code true} if the first operand matches the regex specified as the
* second operand, otherwise {@code false}
* @throws EvaluationException if there is a problem evaluating the expression
* (e.g. the regex is invalid)
*/
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl leftOp = getLeftOperand();
SpelNodeImpl rightOp = getRightOperand();
Object left = leftOp.getValue(state, String.class);
Object right = getRightOperand().getValueInternal(state).getValue();
if (!(left instanceof String)) {
throw new SpelEvaluationException(leftOp.getStartPosition(), SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, left);
}
if (!(right instanceof String)) {
throw new SpelEvaluationException(rightOp.getStartPosition(), SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
}
try {
String leftString = (String) left;
String rightString = (String) right;
Pattern pattern = this.patternCache.get(rightString);
if (pattern == null) {
pattern = Pattern.compile(rightString);
this.patternCache.putIfAbsent(rightString, pattern);
}
Matcher matcher = pattern.matcher(leftString);
return BooleanTypedValue.forValue(matcher.matches());
} catch (PatternSyntaxException ex) {
throw new SpelEvaluationException(rightOp.getStartPosition(), ex, SpelMessage.INVALID_PATTERN, right);
}
}
use of java.util.regex.PatternSyntaxException in project robovm by robovm.
the class Pattern2Test method testCapturingGroups.
public void testCapturingGroups() throws PatternSyntaxException {
Pattern p;
Matcher m;
// Test simple capturing groups
p = Pattern.compile("(a+)b");
m = p.matcher("aaaaaaaab");
assertTrue(m.matches());
assertEquals(1, m.groupCount());
assertEquals("aaaaaaaa", m.group(1));
p = Pattern.compile("((an)+)((as)+)");
m = p.matcher("ananas");
assertTrue(m.matches());
assertEquals(4, m.groupCount());
assertEquals("ananas", m.group(0));
assertEquals("anan", m.group(1));
assertEquals("an", m.group(2));
assertEquals("as", m.group(3));
assertEquals("as", m.group(4));
// Test grouping without capture (?:...)
p = Pattern.compile("(?:(?:an)+)(as)");
m = p.matcher("ananas");
assertTrue(m.matches());
assertEquals(1, m.groupCount());
assertEquals("as", m.group(1));
try {
m.group(2);
fail("expected IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ioobe) {
// expected
}
// Test combination of grouping and capture
// TODO
// Test \<num> sequence with capturing and non-capturing groups
// TODO
// Test \<num> with <num> out of range
p = Pattern.compile("((an)+)as\\1");
m = p.matcher("ananasanan");
assertTrue(m.matches());
try {
p = Pattern.compile("((an)+)as\\4");
fail("expected PatternSyntaxException");
} catch (PatternSyntaxException pse) {
// expected
}
}
use of java.util.regex.PatternSyntaxException in project robovm by robovm.
the class PatternSyntaxExceptionTest method testSerializationCompatibility.
public void testSerializationCompatibility() throws Exception {
PatternSyntaxException object = new PatternSyntaxException("TESTDESC", "TESTREGEX", 3);
SerializationTest.verifyGolden(this, object, PATTERNSYNTAXEXCEPTION_COMPARATOR);
}
Aggregations