Last active
November 2, 2016 23:51
-
-
Save azagniotov/fdca8377dac606a0399178940a015825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.util.regex.Pattern; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
public class NegativeRegexTest { | |
@Test | |
public void shouldTest() throws Exception { | |
final Pattern pattern = Pattern.compile("\\{\"title\":\"(.*)\",\"firstName\":\"((?!john).*)\"\\}"); | |
assertTrue(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"Jeff\"}").matches()); | |
assertTrue(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"stubby4j\"}").matches()); | |
assertTrue(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"jon\"}").matches()); | |
assertTrue(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"anything but john\"}").matches()); | |
assertFalse(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"john of everything\"}").matches()); | |
assertFalse(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"john\"}").matches()); | |
assertFalse(pattern.matcher("{\"title\":\"Ms\",\"firstName\":\"john of Denmark\"}").matches()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment