Skip to content

Instantly share code, notes, and snippets.

@azagniotov
Last active November 2, 2016 23:51
Show Gist options
  • Save azagniotov/fdca8377dac606a0399178940a015825 to your computer and use it in GitHub Desktop.
Save azagniotov/fdca8377dac606a0399178940a015825 to your computer and use it in GitHub Desktop.
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