use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.
the class RegexTesterResource method doTestRegex.
private RegexTesterResponse doTestRegex(String example, String regex) {
final Pattern pattern;
try {
pattern = Pattern.compile(regex, Pattern.DOTALL);
} catch (PatternSyntaxException e) {
throw new BadRequestException("Invalid regular expression: " + e.getMessage(), e);
}
final Matcher matcher = pattern.matcher(example);
boolean matched = matcher.find();
// Get the first matched group.
final RegexTesterResponse.Match match;
if (matched && matcher.groupCount() > 0) {
match = RegexTesterResponse.Match.create(matcher.group(1), matcher.start(1), matcher.end(1));
} else {
match = null;
}
return RegexTesterResponse.create(matched, match, regex, example);
}
use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.
the class RegexTesterResourceTest method regexTesterReturnsValidResponseIfRegExMatches.
@Test
public void regexTesterReturnsValidResponseIfRegExMatches() throws Exception {
final RegexTesterResponse response = resource.regexTester("([a-z]+)", "test");
assertThat(response.matched()).isTrue();
assertThat(response.regex()).isEqualTo("([a-z]+)");
assertThat(response.string()).isEqualTo("test");
assertThat(response.match()).isEqualTo(RegexTesterResponse.Match.create("test", 0, 4));
}
use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.
the class RegexTesterResourceTest method testRegexReturnsValidResponseIfRegExMatches.
@Test
public void testRegexReturnsValidResponseIfRegExMatches() throws Exception {
final RegexTesterResponse response = resource.testRegex(RegexTestRequest.create("test", "([a-z]+)"));
assertThat(response.matched()).isTrue();
assertThat(response.regex()).isEqualTo("([a-z]+)");
assertThat(response.string()).isEqualTo("test");
assertThat(response.match()).isEqualTo(RegexTesterResponse.Match.create("test", 0, 4));
}
use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.
the class RegexTesterResourceTest method regexTesterReturnsValidResponseIfRegExDoesNotMatch.
@Test
public void regexTesterReturnsValidResponseIfRegExDoesNotMatch() throws Exception {
final RegexTesterResponse response = resource.regexTester("([0-9]+)", "test");
assertThat(response.matched()).isFalse();
assertThat(response.regex()).isEqualTo("([0-9]+)");
assertThat(response.string()).isEqualTo("test");
assertThat(response.match()).isNull();
}
use of org.graylog2.rest.models.tools.responses.RegexTesterResponse in project graylog2-server by Graylog2.
the class RegexTesterResourceTest method testRegexReturnsValidResponseIfRegExDoesNotMatch.
@Test
public void testRegexReturnsValidResponseIfRegExDoesNotMatch() throws Exception {
final RegexTesterResponse response = resource.testRegex(RegexTestRequest.create("test", "([0-9]+)"));
assertThat(response.matched()).isFalse();
assertThat(response.regex()).isEqualTo("([0-9]+)");
assertThat(response.string()).isEqualTo("test");
assertThat(response.match()).isNull();
}
Aggregations