Add examples (#10961)

This commit is contained in:
mdhtr
2021-06-27 03:10:43 +02:00
committed by GitHub
parent 7d112b4bc6
commit 7af04a0344

View File

@@ -0,0 +1,61 @@
package com.baeldung.regex.lookaround;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
public class LookaroundUnitTest {
@Test
void givenPositiveLookahead_thenSuccess() {
Pattern pattern = Pattern.compile("import (?=static.+).+");
Matcher matcher = pattern.matcher("import static org.junit.jupiter.api.Assertions.assertEquals;");
assertTrue(matcher.find());
assertEquals("import static org.junit.jupiter.api.Assertions.assertEquals;", matcher.group());
assertFalse(pattern.matcher("import java.util.regex.Matcher;")
.find());
}
@Test
void givenNegativeLookahead_thenSuccess() {
Pattern pattern = Pattern.compile("import (?!static.+).+");
Matcher matcher = pattern.matcher("import java.util.regex.Matcher;");
assertTrue(matcher.find());
assertEquals("import java.util.regex.Matcher;", matcher.group());
assertFalse(pattern.matcher("import static org.junit.jupiter.api.Assertions.assertEquals;")
.find());
}
@Test
void givenPositiveLookbehind_thenSuccess() {
Pattern pattern = Pattern.compile(".*(?<=jupiter).*assertEquals;");
Matcher matcher = pattern.matcher("import static org.junit.jupiter.api.Assertions.assertEquals;");
assertTrue(matcher.find());
assertEquals("import static org.junit.jupiter.api.Assertions.assertEquals;", matcher.group());
assertFalse(pattern.matcher("import static org.junit.Assert.assertEquals;")
.find());
}
@Test
void givenNegativeLookbehind_thenSuccess() {
Pattern pattern = Pattern.compile(".*(?<!jupiter.{0,30})assertEquals;");
Matcher matcher = pattern.matcher("import static org.junit.Assert.assertEquals;");
assertTrue(matcher.find());
assertEquals("import static org.junit.Assert.assertEquals;", matcher.group());
assertFalse(pattern.matcher("import static org.junit.jupiter.api.Assertions.assertEquals;")
.find());
}
}