use of java.util.regex.MatchResult in project jersey by jersey.
the class UriRoutingContext method getFinalMatchingGroup.
@Override
public String getFinalMatchingGroup() {
final MatchResult mr = matchResults.peek();
if (mr == null) {
return null;
}
final String finalGroup = mr.group(mr.groupCount());
// See also PatternWithGroups.match(CharSequence) implementation.
return finalGroup == null ? "" : finalGroup;
}
use of java.util.regex.MatchResult in project intellij-community by JetBrains.
the class PropertiesUtil method getDefaultBaseName.
@NotNull
static String getDefaultBaseName(@NotNull final VirtualFile file) {
final String name = file.getName();
if (!StringUtil.containsChar(name, '_')) {
return FileUtil.getNameWithoutExtension(name);
}
final Matcher matcher = LOCALE_PATTERN.matcher(name);
final String baseNameWithExtension;
int matchIndex = 0;
while (matcher.find(matchIndex)) {
final MatchResult matchResult = matcher.toMatchResult();
final String[] splitted = matchResult.group(1).split("_");
if (splitted.length > 1) {
final String langCode = splitted[1];
if (!LOCALES_LANGUAGE_CODES.getValue().contains(langCode)) {
matchIndex = matchResult.start(1) + 1;
continue;
}
baseNameWithExtension = name.substring(0, matchResult.start(1)) + name.substring(matchResult.end(1));
return FileUtil.getNameWithoutExtension(baseNameWithExtension);
}
}
baseNameWithExtension = name;
return FileUtil.getNameWithoutExtension(baseNameWithExtension);
}
use of java.util.regex.MatchResult in project robovm by robovm.
the class ScannerTest method test_match.
/**
* @tests java.util.Scanner#match()
*/
public void test_match() {
MatchResult result;
s = new Scanner("1 2 ");
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
assertEquals("1", s.next());
assertEquals("2", s.next());
result = s.match();
assertEquals(2, result.start());
assertEquals(3, result.end());
assertEquals(2, result.start(0));
assertEquals(3, result.end(0));
assertEquals("2", result.group());
assertEquals("2", result.group(0));
assertEquals(0, result.groupCount());
try {
result.start(1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
s.next();
fail();
} catch (NoSuchElementException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True faLse");
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
assertTrue(s.nextBoolean());
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
assertFalse(s.nextBoolean());
try {
s.nextBoolean();
fail();
} catch (NoSuchElementException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True faLse");
assertTrue(s.nextBoolean());
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
s.close();
try {
s.nextBoolean();
fail();
} catch (IllegalStateException expected) {
}
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
s = new Scanner("True fase");
assertTrue(s.nextBoolean());
assertEquals(0, result.groupCount());
try {
s.nextBoolean();
fail();
} catch (InputMismatchException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("True fase");
assertTrue(s.nextBoolean());
try {
s.next((Pattern) null);
fail();
} catch (NullPointerException expected) {
}
result = s.match();
assertEquals(0, result.start());
assertEquals(4, result.end());
assertEquals(0, result.start(0));
assertEquals(4, result.end(0));
assertEquals("True", result.group());
assertEquals(0, result.groupCount());
}
use of java.util.regex.MatchResult in project robovm by robovm.
the class ScannerTest method test_findInLine_LPattern.
/**
* @tests java.util.Scanner#findInLine(Pattern)
*/
public void test_findInLine_LPattern() {
Scanner s = new Scanner("");
try {
s.findInLine((Pattern) null);
fail();
} catch (NullPointerException expected) {
}
String result = s.findInLine(Pattern.compile("^"));
assertEquals("", result);
MatchResult matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(0, matchResult.end());
result = s.findInLine(Pattern.compile("$"));
assertEquals("", result);
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(0, matchResult.end());
/*
* When we use the operation of findInLine(Pattern), the match region
* should not span the line separator.
*/
s = new Scanner("aa\nb.b");
result = s.findInLine(Pattern.compile("a\nb*"));
assertNull(result);
s = new Scanner("aa\nbb.b");
result = s.findInLine(Pattern.compile("\\."));
assertNull(result);
s = new Scanner("abcd1234test\n");
result = s.findInLine(Pattern.compile("\\p{Lower}+"));
assertEquals("abcd", result);
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(4, matchResult.end());
result = s.findInLine(Pattern.compile("\\p{Digit}{5}"));
assertNull(result);
try {
matchResult = s.match();
fail();
} catch (IllegalStateException expected) {
}
assertEquals(0, matchResult.start());
assertEquals(4, matchResult.end());
result = s.findInLine(Pattern.compile("\\p{Lower}+"));
assertEquals("test", result);
matchResult = s.match();
assertEquals(8, matchResult.start());
assertEquals(12, matchResult.end());
char[] chars = new char[2048];
Arrays.fill(chars, 'a');
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(chars);
stringBuilder.append("1234");
s = new Scanner(stringBuilder.toString());
result = s.findInLine(Pattern.compile("\\p{Digit}+"));
assertEquals("1234", result);
matchResult = s.match();
assertEquals(2048, matchResult.start());
assertEquals(2052, matchResult.end());
s = new Scanner("test");
s.close();
try {
s.findInLine((Pattern) null);
fail();
} catch (IllegalStateException expected) {
}
s = new Scanner("test1234\n1234 test");
result = s.findInLine(Pattern.compile("test"));
assertEquals("test", result);
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(4, matchResult.end());
int number = s.nextInt();
assertEquals(1234, number);
matchResult = s.match();
assertEquals(4, matchResult.start());
assertEquals(8, matchResult.end());
result = s.next();
assertEquals("1234", result);
matchResult = s.match();
assertEquals(9, matchResult.start());
assertEquals(13, matchResult.end());
result = s.findInLine(Pattern.compile("test"));
assertEquals("test", result);
matchResult = s.match();
assertEquals(14, matchResult.start());
assertEquals(18, matchResult.end());
s = new Scanner("testÂ…\ntest");
result = s.findInLine("est");
assertEquals("est", result);
result = s.findInLine("est");
assertEquals("est", result);
s = new Scanner("test\ntest");
result = s.findInLine("est");
assertEquals("est", result);
result = s.findInLine("est");
assertEquals("est", result);
s = new Scanner("test\n123\ntest");
result = s.findInLine("est");
assertEquals("est", result);
result = s.findInLine("est");
// RI fails. It is a RI's bug.
assertNull(result);
s = new Scanner(" *\n");
result = s.findInLine(Pattern.compile("^\\s*(?:\\*(?=[^/]))"));
assertEquals(" *", result);
}
use of java.util.regex.MatchResult in project robovm by robovm.
the class ScannerTest method test_skip_LPattern.
/**
* @tests java.util.Scanner#skip(Pattern)
*/
public void test_skip_LPattern() {
s = new Scanner("test");
try {
s.skip((String) null);
fail();
} catch (NullPointerException expected) {
}
// If pattern does not match, NoSuchElementException will be thrown out.
s = new Scanner("1234");
try {
s.skip(Pattern.compile("\\p{Lower}"));
fail();
} catch (NoSuchElementException expected) {
}
// Then, no matchResult will be thrown out.
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s.skip(Pattern.compile("\\p{Digit}"));
MatchResult matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(1, matchResult.end());
s.skip(Pattern.compile("\\p{Digit}+"));
matchResult = s.match();
assertEquals(1, matchResult.start());
assertEquals(4, matchResult.end());
s.close();
try {
s.skip(Pattern.compile("test"));
fail();
} catch (IllegalStateException expected) {
}
MockStringReader2Read reader = new MockStringReader2Read("test");
s = new Scanner(reader);
try {
s.skip(Pattern.compile("\\p{Digit}{4}"));
fail();
} catch (NoSuchElementException expected) {
}
try {
s.match();
fail();
} catch (IllegalStateException expected) {
}
s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}"));
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(4, matchResult.end());
s.close();
try {
s.skip((Pattern) null);
fail();
} catch (IllegalStateException expected) {
}
StringBuilder stringBuilder = new StringBuilder();
char[] chars = new char[1024];
Arrays.fill(chars, 'a');
stringBuilder.append(chars);
stringBuilder.append('3');
s = new Scanner(stringBuilder.toString());
s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}"));
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(1025, matchResult.end());
// Large amount of input may be cached
chars = new char[102400];
Arrays.fill(chars, 'a');
stringBuilder = new StringBuilder();
stringBuilder.append(chars);
s = new Scanner(stringBuilder.toString());
s.skip(Pattern.compile(".*"));
matchResult = s.match();
assertEquals(0, matchResult.start());
assertEquals(102400, matchResult.end());
// skip something without risking a NoSuchElementException
s.skip(Pattern.compile("[ \t]*"));
matchResult = s.match();
assertEquals(102400, matchResult.start());
assertEquals(102400, matchResult.end());
}
Aggregations