Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.regex.Pattern;
  3.  
  4. import static java.util.Arrays.asList;
  5. import static java.util.Collections.singletonList;
  6.  
  7. public class Regex1 {
  8.     private static final String NUMBER_REGEX = "^-?[\\d\\.e,]*$";
  9.  
  10.     public static void main(String[] args) {
  11.         Regex1 regex = new Regex1();
  12.         regex.shouldMatchNumberRegex();
  13.     }
  14.  
  15.     private void shouldMatchNumberRegex() {
  16.         List<String> shouldMatch = asList("3.14529", "-255.34", "128", "1.9e10", "123,340.00");
  17.         List<String> shouldNotMatch = singletonList("720p");
  18.  
  19.         for (String s : shouldMatch) {
  20.             Pattern pattern = Pattern.compile(NUMBER_REGEX);
  21.             if (!pattern.matcher(s).matches()) {
  22.                 throw new IllegalStateException("Should match regex!");
  23.             }
  24.         }
  25.  
  26.         for (String s : shouldNotMatch) {
  27.             Pattern pattern = Pattern.compile(NUMBER_REGEX);
  28.             if (pattern.matcher(s).matches()) {
  29.                 throw new IllegalStateException("Should not match regex!");
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement